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

Inside cubebyreference nptr is used nptr is number

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.
Image of page 9
©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
Image of page 10
©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
Image of page 11
©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
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 16 pages?

  • Spring '18
  • Aman
  • cubeByReference

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

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