Download Understanding Pointers and Reference Passing in C and more Slides Computer Fundamentals in PDF only on Docsity! Pointers
int val = 5:
C *otr = &val
OE 5
0x83 OxFE
docsity.com
Pointers
Passing Pointer to function
Pointers and Arrays
Array of Pointer
Dynamic Memory Allocation
docsity.com
. Passing arguments by address void func ( int *pValue ) { *pValue = 6; } void main ( ) { int nValue = 5; printf(“nValue = %d\n“, nValue); func (&nValue ); printf("nValue = %d\n”, nValue); } // 5 gets printed, then 6 gets printed ` nValue 5 pValue 00EF1 00EF1 00AA1 docsity.com . Using the const Qualifier with Pointers • const qualifier – Variable cannot be changed – Use const if function does not need to change a variable – Attempting to change a const variable produces an error • const pointers – Point to a constant memory location – Must be initialized when defined – int *const myPtr = &x; • Type int *const – constant pointer to an int – const int *myPtr = &x; • Regular pointer to a const int – const int *const Ptr = &x; • const pointer to a const int • x can be changed, but not *Ptr docsity.com
on oak wnr =
9
10
11
12
13
14
15
16
17
18
19
20
21
e
/* Fig. 7.10: fig07_10.c
Converting lowercase letters to uppercase letters
using a non-constant pointer to non-constant data */
#include <stdio.h>
#include <ctype.h>
void convertToUppercase( char *sPtr ); /* prototype */
int main()
{
char string[] = “characters and $32.98"; /* initialize char array */
printfC "The string before conversion is: %s", string );
convertToUppercase( string );
printf( "\nThe string after conversion is: %s\n", string );
return 0; /* indicates successful termination */
} /* end main */
Outline
fig07_10.c (Part 1 of
2)
docsity.com
Outline . fig07_11.c (Part 2 of 2) Program Output The string is: print characters of a string docsity.com
=> © ON DA FWD =
0
12
13
14
15
16
17
18
19
20
21
22
23
/* Fig. 7.12: fig07_12.c
Attempting to modify data through a
non-constant pointer to constant data. */
#include <stdio.h>
void f( const int *xPtr ); /* prototype */
int main®
{
int y; /* define y */
FC ay ); /* f attempts illegal modification */
return 0; /* indicates successful termination */
} /* end main */
/* xPtr cannot be used to modify the
value of the variable to which it points */
void f( const int *xPtr )
{
*xptr = 100; /* error: cannot modify a const object */
} /* end function f */
A Outline
Vv
fig07_12.c
docsity.com
Outline . Program Output Compiling... FIG07_12.c d:\books\2003\chtp4\examples \ch07\fig07_12.c(22) : error C2166: l-value specifies const object Error executing cl.exe. FIG07_12.exe - 1 error(s), 0 warning(s) docsity.com Outline . Program Output Compiling... FIG07_14.c D:\books\2003\chtp4\Examples \ch07\FIG07_14.c(17) : error C2166: l-value specifies const object D:\books\2003\chtp4\Examples \ch07\FIG07_14.c(18) : error C2166: l-value specifies const object Error executing cl.exe. FIG07_12.exe - 2 error(s), 0 warning(s) docsity.com . Swap temp = x ; x = y ; y = temp ; docsity.com . void swap ( int * , int * ); // function prototype void main ( ) { int x = 10 , y = 20 ; printf(“Before calling swap \n”); printf(“value of x=%d and y=%y \n”,x,y); swap ( &x , &y ) ; // function call printf(“After calling swap \n”); printf(“value of x=%d and y=%y \n”,x,y); } Swap using function docsity.com