Download presentation
Presentation is loading. Please wait.
1
Pointers and Pass By Reference
4/29/16 1
2
Pass by Reference Pass-by-value
Values from call copied into parameters Only the value returned communicates back to the main, or calling function. byValue.c Pass-by-reference Passes the address of the argument variable. The called function can reference, or access, the variable using the passed address. Also referred to as a call by reference 2 2
3
Addresses Before using pass-by-reference we need to look at addresses in C programs. 3 3
4
Using Addresses in a Program
#include <stdio.h> int main() { int a = 5; printf("a is %d\n", a); printf("address of a is %p\n",&a ); return 0; { Output is: a is 5 address of a is 0x7fffaa63e41c Address is hexadecimal, or base 16 4 4
5
Storing Addresses numAddr = # A variable that can store an address is known as a pointer variable or pointer 5 5
6
Declaring and Using Pointers
In declaring a pointer variable, C requires that we also specify the type of variable that is pointed to int *numAddr; This declaration can be read in a number of ways: as the variable pointed to by numAddr is an integer, or as numAddr points to an integer 6 6
7
Using Addresses Dereferencing operator: *
*numAddr means the variable whose address is stored in numAddr Or, the variable pointed to by numAddr When using a pointer, the value obtained is always found by first going to the pointer for an address. 7 7
8
Using Addresses (continued)
8 8
9
Declaring and Using Pointers
#include <stdio.h> int main() { int a = 5; int *p = NULL; //declare p as a pointer //p points at nothing p = &a; //Give p the address of a //pointing p at a *p = 8; //derefence p to access a printf("Now a is %d\n", a); return 0; } OUTPUT: Now a is 8 9 9
10
Declaring and Using Pointers (continued)
10 10
11
Passing without Pointers
#include <stdio.h> #include <stdlib.h> void ConvFeetInches(int totDist, int inFeet, int inInches) { inFeet = totDist / 12; inInches = totDist % 12; return; } int main(void) { int initMeasure = 45; int resFeet = 0; int resIn = 0; ConvFeetInches(initMeasure, resFeet, resIn); printf("%d feet %d inches\n", resFeet, resIn); return 0; //ch8/feetInch1.c 11 11
12
Passing Addresses to a Function (continued)
12 12
13
Passing Addresses to a Function
#include <stdio.h> #include <stdlib.h> void ConvFeetInches(int totDist, int* inFeet, int* inInches) { *inFeet = totDist / 12; *inInches = totDist % 12; return; } int main(void) { int initMeasure = 45; int resFeet = 0; int resIn = 0; ConvFeetInches(initMeasure, &resFeet, &resIn); printf("%d feet %d inches\n", resFeet, resIn); return 0; //ch8/feetInch2.c 13 13
14
Another Example Given the radius of the circle, have a function find the area and circumference. circle.c is started 14 14
15
Participation Write a C function the has reference parameters for the length and width of a rectangle. The function should enlarge the size of the rectangle up by a factor of 4. It should multiply the length and width being pointed to by the parameters by 4.
16
Summary A pointer is a variable or parameter that is used to store the address of another variable If a parameter or variable is a pointer, then the dereferencing operator, *, must be used to access the variable whose address is stored in the pointer The address of a variable can be passed to a function When a called function receives an address, it has the capability of directly accessing the respective calling function’s variable 16 16
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.