chapter7 pointers
TRANSCRIPT
-
7/29/2019 Chapter7 Pointers
1/20
Principles of Programming - NI2005 1
Chapter 7: Pointers
In this chapter you will learn about,
Basic concept of pointers
Pointer variables
Pointer declaration
Pointer definition and initializationPointer operator (& and *)
Parameter passing by reference
-
7/29/2019 Chapter7 Pointers
2/20
Principles of Programming - NI2005 2
What is a pointer
So far, we have seen that a variable is used to storea value.
Variables allow the programmer to directlymanipulate the data in memory.
A pointer variable, however, does not store a valuebut store the addressof thememory space whichcontain the value i.e. it directly points to a specificmemory address.
Why would we want to use pointers?
To call a function by reference so that the data passedto the function can be changed inside the function.
To create a dynamic data structure which can growlarger or smaller as necessary.
-
7/29/2019 Chapter7 Pointers
3/20
Principles of Programming - NI2005 3
Variable declaration
A variable declaration such as,int number = 20; causes the compiler to
allocate a memory location for the variable
numberand store in it the integer value 20.
This absolute address of the memory locationis readily available to our program during the
run time.
The computer uses this address to access its
content.number
20number directly references a
variable whose value is 7
11001100
-
7/29/2019 Chapter7 Pointers
4/20
Principles of Programming - NI2005 4
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 beingdeclared is a pointer variable instead of anormal variable.
-
7/29/2019 Chapter7 Pointers
5/20
Principles of Programming - NI2005 5
Pointer declaration cont
Consider the following declaration
int *numberPtr, number = 20;
In this case, two memory address have beenreserved in the memory, namely the
numberPtr and number.
The value in variable number is of typeinteger, and the value in variable numberPtris an address for another memory.
20
1100110011111111
number*numberPtr
-
7/29/2019 Chapter7 Pointers
6/20
Principles of Programming - NI2005 6
Pointer Initialization
To prevent the pointer from pointing to arandom memory address, it is advisable thatthe pointer is initialized to 0, NULL oranaddress before being used.
A pointer with the value NULL, points tonothing.
Initializing a pointer to 0 is equivalent toinitializing a pointer to NULL, but NULL is
preferred.
-
7/29/2019 Chapter7 Pointers
7/20
Principles of Programming - NI2005 7
Pointer Operator (& and *)When a pointer is created, it is not pointing to any
valid memory address. Therefore, we need to assignit to a variables address by using the & operator.This operator is called a reference operator.
Look at this example:
int number = 20;int *numberPtr;
numberPtr = &number;
printf(number = %d, *numberPtr);
The statement numberPtr = &numberassigns theaddress of the variable number to a pointer variablenumberPtr. Variable numberPtr is then said as topoint to variable number.
Output:
number = 20
-
7/29/2019 Chapter7 Pointers
8/20
Principles of Programming - NI2005 8
Graphical representation
int *numberPtr, number = 20;
numberPtr = &number;
20
1100110011111111
number*numberPtr
2011001100
1100110011111111
number*numberPtr
-
7/29/2019 Chapter7 Pointers
9/20
Principles of Programming - NI2005 9
Pointer Operator (& and *) cont
After a pointer is assigned to a particularaddress, the value in the pointed address canbe accessed/modified using the * operator.
This operator is commonly called as theindirection operatorordereferencing
operator.The * operator returns the value of the objectto which its operand points. For example, thestatement
printf(number = %d, *numberPtr);prints the value of variable number, namely as20. Using * in this manner is calleddereferencing operator.
-
7/29/2019 Chapter7 Pointers
10/20
Principles of Programming - NI2005 10
Example: & and *#include
void main(void){
int var = 10;
int *ptrvar = &var;
printf(The address of the variable var is: %d\n, &var);
printf(The value of the pointer ptrvar is: %d\n, ptrvar);
printf(Both values are the same\n);
printf(The value of the variable var is: %d\n, var);
printf(The value of *ptrvar is: %d\n, *ptrvar);
printf(Both values are the same\n);
printf(The address of the value pointed by ptrvar is: %d\n, &*ptrvar);
printf(The value inside the address of ptrvar is: %d\n, *&ptrvar);
printf(Both values are the same\n);
}
-
7/29/2019 Chapter7 Pointers
11/20
Principles of Programming - NI2005 11
Example: & and */*Sample Output */
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: 1245052The value inside the address of ptrvar is: 1245052
Both values are the same
Press any key to continue
-
7/29/2019 Chapter7 Pointers
12/20
Principles of Programming - NI2005 12
&* and *&
& and * are inverse operations. &* actsequivalent to *& and this leads back to theoriginal value.
Example: (Assume that the address of num is1245052)
#include
void main(void){
int num = 5;int *numPtr = #
printf("%d \n", numPtr);printf("%d \n", &*numPtr);printf("%d \n", *&numPtr);
}
Output:
1245052
1245052
1245052
-
7/29/2019 Chapter7 Pointers
13/20
Principles of Programming - NI2005 13
Parameter Passing by Reference/Pointer
A function may return multiple values bydeclaring their formal parameters (passingvalue) as pointers variables.
This way of passing the argument is knownas call by reference
When the value referenced by the pointer ischanged inside the function, the value in theactual variable will also change.
Therefore, we can pass the result of thefunction through the function argumentwithout having to use the return statement.
-
7/29/2019 Chapter7 Pointers
14/20
Principles of Programming - NI2005 14
Parameter Passing by Reference/Pointer
When a pointer is passed to a function, weare actually passing the address of a variableto the function.
Since we have the address, we can directlymanipulate the data in the address.
In the case where a non-pointer variable ispassed, the function will create another spacein memory to hold the value locally while the
program is inside the function. Therefore, anychange to the variable inside the function willnot change the actual value of the variable.
-
7/29/2019 Chapter7 Pointers
15/20
Principles of Programming - NI2005 15
Parameter Passing by Reference/Pointer
To pass the value of variable by pointersbetween two functions, we must do thefollowing:
Declare the variable that is meant to return a
value to the calling function as a pointer
variable in the formal parameter list of the
function.
void function_name(int *varPtr);
When to call the function, use a variabletogether with address operator (&)
function_name(&var);
-
7/29/2019 Chapter7 Pointers
16/20
Principles of Programming - NI2005 16
Parameter Passing by Reference/Pointer
Note : the effect of the previous two actions isthe same as the effect of the declarations
int var;
int *varPtr=&var;
Declare the function with pointer parameterappropriately in a function prototype by using
symbol * as a prefix for pointer parameters.
void function_name(int *);
-
7/29/2019 Chapter7 Pointers
17/20
Principles of Programming - NI2005 17
Example
#include
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);
}
-
7/29/2019 Chapter7 Pointers
18/20
Principles of Programming - NI2005 18
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);
}
-
7/29/2019 Chapter7 Pointers
19/20
Principles of Programming - NI2005 19
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
After Func2 is called, a = 0, b = 0
Press any key to continue
-
7/29/2019 Chapter7 Pointers
20/20
Principles of Programming - NI2005 20
SummaryIn 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 bypointers 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.