Chapter7 Pointers
Chapter7 Pointers
In this chapter you will learn about, Basic concept of pointers Pointer variables
Pointer declaration Pointer definition and initialization
What is a pointer
So far, we have seen that a variable is used to store a value.
Variable declaration
A variable declaration such as,
int number = 20; causes the compiler to allocate a memory location for the variable number and store in it the integer value 20. This absolute address of the memory location is readily available to our program during the run time. The computer uses this address to access its content.
number 20 11001100
Principles of Programming - NI2005
Pointer declaration
General Format:
data_type *pointer_name;
A pointer declaration such as, int *numberPtr; declares numberptr as a variable that points to an integer variable. Its content is a memory address. The * indicates that the variable being declared is a pointer variable instead of a normal variable.
Principles of Programming - NI2005 4
The value in variable number is of type integer, and the value in variable numberPtr is an address for another memory.
*numberPtr number
20
11111111 11001100
Pointer Initialization
To prevent the pointer from pointing to a random memory address, it is advisable that the pointer is initialized to 0, NULL or an address before being used. A pointer with the value NULL, points to nothing. Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred.
Output:
number = 20
The statement numberPtr = &number assigns the address of the variable number to a pointer variable numberPtr. Variable numberPtr is then said as to point to variable number.
Principles of Programming - NI2005
Graphical representation
int *numberPtr, number = 20;
*numberPtr number
20
11111111 11001100
numberPtr = &number;
*numberPtr number
11001100
11111111
20
11001100
The address of the variable var is: 1245052 The value of the pointer ptrvar is: 1245052 Both values are the same
The value of the variable var is: 10 The value of *ptrvar is: 10 Both values are the same The address of the value pointed by ptrvar is: 1245052 The value inside the address of ptrvar is: 1245052 Both values are the same Press any key to continue
Principles of Programming - NI2005 11
12
When the value referenced by the pointer is changed inside the function, the value in the actual variable will also change.
Therefore, we can pass the result of the function through the function argument without having to use the return statement.
Principles of Programming - NI2005 13
In the case where a non-pointer variable is passed, the function will create another space in memory to hold the value locally while the program is inside the function. Therefore, any change to the variable inside the function will not change the actual value of the variable.
14
When to call the function, use a variable together with address operator (&)
function_name(&var);
15
Declare the function with pointer parameter appropriately in a function prototype by using symbol * as a prefix for pointer parameters.
void function_name(int *);
16
Example
#include <stdio.h> void Func1(int, int); void Func2(int *, int *); void main( ) { int a = 8, b = 9; printf(Before Func1 is called, a = %d, b = %d\n, a, b); Func1(a, b); printf(After Func1 is called, a = %d, b = %d\n, a, b);
printf(\nBefore Func2 is called, a = %d, b = %d\n, a, b); Func2(&a, &b); printf(After Func2 is called, a = %d, b = %d\n, a, b);
}
Principles of Programming - NI2005 17
Example
void Func1(int a, int b) { a = 0; b = 0; printf(The value inside Func1, a = %d, b = %d\n, a, b); } void Func2(int *pa, int *pb) { *pa = 0; *pb = 0; printf("The value inside Func2, *pa = %d, *pb = %d\n\n", *pa, *pb); }
18
Result
/* output */
Before Func1 is called, a = 8, b = 9 The value inside Func1, a = 0, b = 0 After Func1 is called, a = 8, b = 9
Before Func2 is called, a = 8, b = 9 The value inside Func2, *pa = 0, *pb = 0
Summary
In this chapter, you have learnt
The pointer operator: & and * Pointer declaration and assignment Parameter passing by reference
The 3 steps needed to pass the value of variable by pointers between two functions.
Declare the variable as a pointer variable in formal parameter list of the function. Use a variable together with address operator (&) when to call a function. Using symbol * in function prototype to declare any pointer parameter.
20