Inside cubeByReference nPtr is used nPtr is number Notice that the function

# Inside cubebyreference nptr is used nptr is number

• 16

This preview shows page 9 - 13 out of 16 pages.

Inside cubeByReference , *nPtr is used (*nPtr is number ). Notice that the function prototype takes a pointer to an integer. The original value of number is 5 The new value of number is 125 Program to call a function by reference.
©LPU CSE101 C Programming Fig. Analysis of a typical call-by-reference with a pointer argument. 125 void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } int main() { int number = 5 ; cubeByReference( &number ); } void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } int main() { int number = 5 ; cubeByReference( &number ); } int main() { int number = 5 ; cubeByReference( &number ); } number 5 nPtr number 5 nPtr number 125 nPtr Before function call After function receives the call and before function computes After *nptr is cubed and before program control returns to main : undefined call establishes this pointer called function modifies caller’s variable
©LPU CSE101 C Programming The Relationship Between Pointers and Arrays Arrays and pointers closely related Array name is like a constant pointer Pointers can do array subscripting operations Define an array b[5] and a pointer bPtr To set bPtr to point to b[5]: bPtr = b; The array name ( b ) is actually the address of first element of the array b[5] which is equivalent to bPtr = &b[0] Explicitly assigns bPtr to address of first element of b
©LPU CSE101 C Programming The Relationship Between Pointers and Arrays Element b[3] Can be accessed by *(bPtr + 3) Where 3 is the offset. Called pointer/offset notation Can be accessed by

#### You've reached the end of your free preview.

Want to read all 16 pages?

• Spring '18
• Aman
• cubeByReference

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes