










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of pointers in c programming language, including pointer initialization, operators (* and &), and passing arguments to functions by value, reference, and address. It includes examples and program outputs.
Typology: Slides
1 / 18
This page cannot be seen from the preview
Don't miss anything!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 /* Fig. 7.4: fig07_04.c
2 Using the & and * operators */
3 #include <stdio.h>
4
5 int main()
6 {
7 int a; /* a is an integer */
8 int aPtr; / aPtr is a pointer to an integer */
9
10 a = 7 ;
11 aPtr = &a; /* aPtr set to address of a */
12 13 printf( "The address of a is %p"
14 "\nThe value of aPtr is %p", &a, aPtr );
15
16 printf( "\n\nThe value of a is %d"
17 "\nThe value of *aPtr is %d", a, *aPtr );
18
19 printf( "\n\nShowing that * and & are complements of "
20 "each other\n&*aPtr = %p"
21 "\n&aPtr = %p\n", &aPtr, *&aPtr );
22
23 return 0 ; /* indicates successful termination */
24
25 } /* end main */
The address of a is the value fig07_04.c of aPtr.
The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a.
Notice how * and & are inverses
must be used
Generic pointer, represents any type
No casting needed to convert a pointer to
void pointer
void pointers cannot be dereferenced
Call by reference with pointer arguments
* operator
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 docsity.com
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Program Output
The original value of number is 5 The new value of number is 125
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 /* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference with a pointer argument */ 3 4 #include <stdio.h> 5 6 void cubeByReference( int nPtr ); / prototype / 7 8 int main() 9 { 10 int number = 5 ; / initialize number / 11 12 printf( "The original value of number is %d", number ); 13 14 / pass address of number to cubeByReference / 15 cubeByReference( &number ); 16 17 printf( "\nThe new value of number is %d\n", number ); 18 19 return 0 ; / indicates successful termination / 20 21 } / end main / 22 23 / calculate cube of *nPtr; modifies variable number in main */ 24 void cubeByReference( int *nPtr ) 25 { 26 *nPtr = *nPtr * *nPtr * nPtr; / cube *nPtr / 27 } / end function cubeByReference */
fig07_07.c
Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable).
Inside cubeByReference, nPtr is used (nPtr is number).
Notice that the function prototype takes a pointer to an integer.