Download presentation
Presentation is loading. Please wait.
1
Topic 8 – Introduction to Pointers and Function Output Parameters
2
CISC105 – Topic 8 Variables and Memory When a variable is declared, the operating system will find a location in memory in which to store that variable. Each variable exists at a specific memory location and each memory location has a unique address. Therefore, each variable exists at a unique memory location and a unique address identifies that location.
3
CISC105 – Topic 8 The & Operator As seen previously in the scanf function, the & operator means “the address of.” Thus, evaluates to address of the variable big_var1. &big_var1
4
CISC105 – Topic 8 Pointers There is another set of data types, that are capable of holding addresses of variables. They are called pointers. This is indicated by prefacing the name of the variable with a “*”. Examples include: char *p_letterGrade; int *p_num1; double*p_num2;
5
CISC105 – Topic 8 Pointers This says that p_letterGrade holds the address of a character. Similarly, p_num1 holds the address of a integer. Finally, p_num2 holds the address of a double-precision floating point number. char *p_letterGrade; int *p_num1; double*p_num2;
6
CISC105 – Topic 8 Pointers The new data types that we are identifying are known as pointers. As each one will contain the address of a variable, we can think of them as pointing to that variable. Note that each name starts with a “ p_ ” indicating that they are pointers. Picking some naming convention so that pointers can be easily identified is a very good programming practice.
7
CISC105 – Topic 8 Pointers p_letterGrade is a pointer to a character, or a character pointer. p_num1 is a pointer to a integer, or an integer pointer. p_num2 is a pointer to a double, or a double- precision floating point number pointer. char *p_letterGrade; int *p_num1; double*p_num2;
8
CISC105 – Topic 8 Pointers: A Definition A pointer is simply a memory address. It can be used to access whatever exists at that memory address.
9
CISC105 – Topic 8 A Simple Pointer Example Therefore, suppose we wish to create two integer variables, and also have pointers to those two variables. int num1, num2; int *p_num1, *p_num2; p_num1 = &num1; p_num2 = &num2; First, we declare the variables. They are placed somewhere in memory. Next, we declare two variables that are pointers to integers. This statement finds the address of num1 and stores it in p_num1. This statement finds the address of num2 and stores it in p_num2.
10
CISC105 – Topic 8 A Simple Pointer Example int num1, num2; int *p_num1, *p_num2; p_num1 = &num1; p_num2 = &num2; 1004 1006 1008 1010 1012 1014 1016 1018 (num2) (num1)(p_num1) (p_num2) 1014 1008
11
CISC105 – Topic 8 So…how is a Pointer used? A pointer can be used via the indirection operator (or dereferencing operator), “*”. The * operator simply means “follow the pointer.” When a pointer is “followed”, this is known as dereferencing the pointer. A *(pointer) expression is equal to the value of the variable that the pointer points to.
12
CISC105 – Topic 8 The * Operator: A Simple Example In the previous example (slightly modified): int num1 = 9, num2 = 21, answer; int *p_num1, *p_num2; p_num1 = &num1; p_num2 = &num2; answer = num1 + *p_num2; num1 = num2 = *p_num1 = *p_num2 = answer = 9 21 9 30
13
CISC105 – Topic 8 Types of References Here, in the final statement, using num1 is called a direct reference, as we simply go the location the num1 is stored at. The use of p_num2 is a indirect reference, as we first go to the location the pointer p_num2 is stored at to get the address of the data, and then we go there. int num1 = 9, num2 = 21, answer; int *p_num1, *p_num2; p_num1 = &num1; p_num2 = &num2; answer = num1 + *p_num2;
14
CISC105 – Topic 8 Call-By-Value Thus far, we have seen one type of function parameter, known as call-by-value. This type of function calling initializes the input parameters to the values passed into the function. The value passed into the function is copied and the copy is stored in the function parameter (named in the function definition header).
15
CISC105 – Topic 8 Call-By-Value Example int x = 27; fun_y(x); … void fun_y(int z) { printf(“z=%d”,z); return; } 1004 1006 1008 1010 1012 1014 1016 1018 (x)27 (z)27 Notice that there are two variable references. First, we make a direct reference to x when we call fun_y(). Next, we make a direct reference to z when we call printf().
16
CISC105 – Topic 8 Call-By-Reference We can also call a function using what is known as call-by-reference. Instead of copying the value passed into a function into the function parameter, we pass a pointer to the value into the function.
17
CISC105 – Topic 8 Call-By-Reference Example int x = 27; fun_y(&x); … void fun_y(int *p_x) { printf(“x=%d”,*p_x); return; } 1004 1006 1008 1010 1012 1014 1016 1018 (x)27 (p_x)1014 First, we use the & operator on x to get its memory address. Then that address is passed into fun_y() and stored in p_x. Next, we make an indirect reference to x when we call printf(). When this happens, we first look at p_x to get the memory address of x. Then, we go to that location (1014) and get the value of x.
18
CISC105 – Topic 8 Comparison There are obvious differences in these calling methods. First and foremost, in the call-by-value method, the value stored in the variable in the calling function cannot be changed by the called function, as that value is copied into the new variable that is declared in the function parameters of the called function. This copy is the one the called function uses.
19
CISC105 – Topic 8 Here, the function fun_y() knows nothing about the variable x. Thus, any statement inside fun_y() cannot use x, they must use z. So, when fun_y() modifies z, the value stored in x is not modified. Call-By-Value Example II int x = 27; fun_y(x); … void fun_y(int z) { z = z + 1; printf(“z=%d”,z); return; } 1004 1006 1008 1010 1012 1014 1016 1018 (x)27 (z)2728
20
CISC105 – Topic 8 Comparison In the call-by-reference method, the value stored in the variable stored in the variable in the calling function CAN be changed by the called function, as a pointer to that value is passed into the called function. Thus, both functions use the same value, one directly (the calling function) and one indirectly (the called function).
21
CISC105 – Topic 8 Here, the function fun_y() has a pointer to the memory location of x. Thus, any statement inside fun_y() CAN use x. So, when fun_y() modifies x, the value stored in x IS modified. Call-By-Reference Example II int x = 27; fun_y(&x); … void fun_y(int *p_x) { *p_x = *p_x + 1; printf(“z=%d”, *p_x); return; } 1004 1006 1008 1010 1012 1014 1016 1018 (x)27 (p_x)1014 28
22
CISC105 – Topic 8 Function Output Parameters Notice that the variable x is both an input parameter and output parameter to the function fun_y(). It is initialized to a value and the function uses that value (input parameter). It is also updated by the function; the function stores a value there (output parameter). Notice that if x were to be used again in the calling function, after the call to fun_y(), the new value, in this case 28, would be seen.
23
CISC105 – Topic 8 Function Output Parameters This approach also allows us to return more than one piece of data from a function. This can be accomplished by passing pointers to variables of the type we wish to return. For example, if we wish to return 3 double types, we create three double variables in the calling function and pass 3 pointers to those variables into the called function. Then, the called function stores the output in those variables, using an indirect reference.
24
CISC105 – Topic 8 Function Output Parameters (A Simple Example) – Rev. I double x,y,z; double *p_x, *p_y, *p_z; p_x = &x; p_y = &y; p_z = &z; get_doubles(p_x,p_y,p_z); printf(“x=%f, y=%f, z=%f”,x,y,z); … void get_doubles(double *p2_x, double *p2_y, double *p2_z) { double in1, in2, in3; printf(“Enter three doubles>”); scanf(“%lf %lf %lf”,&in1, &in2, &in3); *p2_x = in1; *p2_y = in2; *p2_z = in3; }
25
CISC105 – Topic 8 Function Output Parameters (A Simple Example) – Rev. II double x,y,z; get_doubles(&x, &y, &z); printf(“x=%f, y=%f, z=%f”,x,y,z); … void get_doubles(double *p2_x, double *p2_y, double *p2_z) { double in1, in2, in3; printf(“Enter three doubles>”); scanf(“%lf %lf %lf”,&in1, &in2, &in3); *p2_x = in1; *p2_y = in2; *p2_z = in3; }
26
CISC105 – Topic 8 Function Output Parameters (A Simple Example) – Rev. III double x,y,z; get_doubles(&x, &y, &z); printf(“x=%f, y=%f, z=%f”,x,y,z); … void get_doubles(double *p2_x, double *p2_y, double *p2_z) { printf(“Enter three doubles>”); scanf(“%lf %lf %lf”, p2_x, p2_y, p2_z); }
27
CISC105 – Topic 8 Common Errors with Pointers Never, EVER, attempt to dereference a pointer before it has been assigned to something (set equal to the address of something). As a variable is in an unknown state (garbage) before it is initialized, this would cause a program to attempt to access some random location in memory, which is not allowed. This often results in the common program run- time error message, “Segmentation Fault.”
28
CISC105 – Topic 8 Common Errors with Pointers Note that this is a run-time error. It will not cause a compiler error. Therefore, make sure you set a pointer equal to the address of some variable (of the correct type!) before you attempt to use it. For example: double x = 10.0, y = 20.0, *p_x, *p_y; p_x = &x; printf(“x = %f and y = %f\n”, *p_x, *p_y); ERROR! This will cause the program to crash as p_y has not been initialized.
29
CISC105 – Topic 8 Common Errors with Pointers Also, a pointer can only point to the “correct” data type. For example, an integer pointer CANNOT point to a float data type. This is a compile-time error. It will generate a compiler error. For example: double x = 10.0, *p_x, *p_y; int y = 20; p_x = &x; p_y = &y printf(“x = %f and y = %f\n”, *p_x, *p_y); ERROR! This is not allowed as y is an int and p_y is a pointer to a double.
30
CISC105 – Topic 8 Example Write a complete function that takes four double values as input and adds the first two values to the second two values. For example, if w=2, x=4, y=6, z=8, after a call to function1(w,x,y,z), w=2, x=4, y=8, and z=12.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.