8_C_Pointers_Part_2.pdf

# In such cases we can in effect pass by reference

• 37

This preview shows pages 33–37. Sign up to view the full content.

` In such cases, we can, in effect, pass by reference , which means we pass a pointer to the parameter . ` This will allow the function to alter the variable which is used to pass the parameter’s value in the calling environment .

This preview has intentionally blurred sections. Sign up to view the full version.

` Consider a simple function to swap, or exchange two values, with the following declaration: void swap(int x, int y);
` Consider using this function to swap, or exchange two values, in the following code: /* Recall the declaration of the function: void swap(int x, int y); */ int a = 10; int b = 5; swap(a, b); ` Even if swap correctly exchanges a and b in the called function, swap(), the value of a and b in this, the calling function will still have their original values ` What to do?

This preview has intentionally blurred sections. Sign up to view the full version.

int a = 10; int b = 5; swap(&a, &b); /*Now, swap will be able to exchange the values of a and b in the calling environment */ NOTICE: The declaration of swap must be changed too: void swap(int *x, int *y); because we are now passing 2 8-byte addresses rather than 2 4-byte integers
` Suppose we want to call a function declared as: int sum(const int *array, int size ); It sums the elements of an array given the address of the start of the array and its size: int array[6] = {18, 16, 15, 20, 19, 17}; int size = 6; int total; . . . . total = sum(array, size); /* OR total = sum(&array[0], size); */ . . . . ` Any time a pointer is passed as a parameter, if the function will not write to variables pointed to by the pointer, the const keyword should be used. This is why the first parameter of sum should be declared with the const keyword above. It will allow the function sum() to read values from the array, but not affect the values in any way.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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