4 - Pointers - Lecture (Slides)
4 - Pointers - Lecture (Slides)
Pointers
1
Why Learning Pointers?
• Pointer is a very powerful tool for the design of C
programs. A pointer is a variable that holds the value of
the address or memory location of another data
object.
• In C, pointers can be used in many ways. These include
the passing of variable’s address to functions to
support call by reference, and the use of pointers for
the processing of arrays and strings.
• In this lecture, we discuss the concepts
of pointers including address operator,
pointer variables and call by reference.
2
Pointers
3
Variables of Primitive Data Types
#include <stdio.h> Variables of
int main() Printing the primitive
{ value of the data types:
int num = 5; variable int, char,
float, etc.
printf("num = %d, \n", num );
}
Note: The variable
Output Address
Memory
4
Variables of Primitive Data Types
#include <stdio.h> Variables of
int main() Printing the primitive
{ value of the data types:
int num = 5; variable int, char,
float, etc.
printf("num = %d, \n", num );
scanf("%d", &num);
printf("num = %d, \n", num );
}
Note: The variable
Output Address
Memory
5
Address Operator (&)
#include <stdio.h> Printing the
int main() memory
{ address of the
int num = 5; variable
printf("num = %d, &num = %p\n", num, &num);
scanf("%d", &num);
printf("num = %d, &num = %p\n", num, &num);
}
Memory
num
Address
Output
1000
10
0 1 0 1 0 1 1 0
num = 5, &num = 1000 [address] 1001
1002
1
0
0
1
1
0
0
1
0 1
1 0
0
1
1
0
Content of
memory
10 1003
1004
6
Primitive Variables: Key Ideas
int num=5;
(1) num
– It is a variable of data type int and
4 bytes of memory are allocated.
Example:
num
– Its memory location is used to
store the integer value of the Memory
variable. &num 5
address
1000
(2) &num
– It refers to the memory address of
the variable which is used to store
the int value of the variable.
8
Pointer Variables: Declaration
• Pointer variable – different from the primitive variable num
(variable of primitive data type such as int, float, char) declared
earlier, it stores the address of memory location of a data object.
• A pointer variable is declared by, for example:
addr
0 1 0 1 0 1 1 0
10
Pointer Variables: Declaration Examples
float *ptrF; - ptrF is a pointer variable. It stores the
address of the memory which is used for
storing a Float value.
Memory float value stored
ptrF 2024
address (4 bytes)
2024
char *ptrC; - ptrC is a pointer variable. It stores the
address of the memory which is used for
storing a Character value.
11
Pointer Variables: Key Ideas
int * ptrI;
(1) ptrI
• Pointer variable (4 bytes of memory).
• The value of the variable (i.e. stored in
the variable) is an address. Address
Memory
ptrI
(2) *ptrI
addr
1000 0 1 0 1 0 1 1 0
1001 Content of
1 0 1 0 0 1 0 1
1002
memory
0 1 0 1 1 0 1 0
• Contains the content (or value) of the 1003
1004
memory location pointed to by the 1005
13
How to use Pointer Variables? (Cont’d.)
int a=20; float b=40.0; char c='a';
int *ptrI; float *ptrF; char *ptrC;
ptrI = &a; => *ptrI == 20 [same as variable a] *ptrI and a – now
ptrF = &b; => *ptrF == 40.0 [same as b] refer to the same
ptrC = &c; => *ptrC == ‘a’ [same as c] memory content
Statement Operation Similarly,
ptrI
*ptrF and b==40.0
int *ptrI ? Uninitialized Pointer *ptrC and c ==‘a’
ptrI a
ptrI = &a; 1000 20 Address = 1000
ptrF b
ptrF = &b; 2000 40.0 Address = 2000
ptrC c
ptrC = &c; 3000 a Address = 3000
ptr
int *ptr = NULL; NULL
14
Pointer Variables – Example 1
#include <stdio.h> St at emen t Operat io n
int main()
ptr num
{ ptr = # 1024 3 Address = 1024
int num = 3; // integer var ptr num
int * ptr; // pointer var *ptr = 10; 1024 10 Address = 1024
}
15 Note: num and *ptr have the same value
Pointer Variables – Example 1 (Cont’d.)
#include <stdio.h> St at emen t Operat io n
int main()
ptr num
{ ptr = # 1024 3 Address = 1024
int num = 3; // integer var ptr num
int *ptr; // pointer var *ptr = 10; 1024 10 Address = 1024
printf("ptr = %p, *ptr = %d\n", ptr, *ptr); num = 10, &num = 1024
[*ptr = 10]
*ptr = 10;
// What will be the values: *ptr, num, &num?
printf("num = %d, &num = %p\n", num, &num);
return 0;
16
}
Pointer Variables – Example 2
/* Example to show the use of pointers */ num1 num2
#include <stdio.h> 1024 3 2048 5
int main()
{
int num1 = 3, num2 = 5; // integer variables ptrl ptr2
int *ptr1, *ptr2; // pointer variables 1024 2048
17
Pointer Variables – Example 2 (Cont’d.)
num1
/* increment by 1 the content of the memory
4
location pointed by ptr1 */
(*ptr1)++; ptrl
// What are the values for num1, *ptr1?
1024
printf(“num1 = %d, *ptr1 = %d\n”, num1, *ptr1);
Output
num1 = 4, *ptr1 = 4
18
Pointer Variables – Example 2 (Cont’d.)
num1 num2
/* copy the content of the location pointed by ptr1
4 4
into the location pointed by ptr2*/
num2 = 4, *ptr2 = 4
19
Pointer Variables – Example 2 (Cont’d.) 1 num2
*ptr2 = 10; 1 /* 10 copied into the location pointed 10
by ptr2 */ 2
2/* copy the content of the num1
num1 = *ptr2;
10 ptr2
memory location pointed by ptr2 2048
into num1 */
ptrl
printf(“num1 = %d,*ptr1 = %d\n”,num1, *ptr1);
1024
Output
num1 = 10, *ptr1 = 10
20
Pointer Variables – Example 2 (Cont’d.)
3
num1
50 4
num2
ptrl 10
1024
3 ptr2
*ptr1 = *ptr1 * 5;
1024
printf(“num1 =%d, *ptr1 = %d\n”,num1, *ptr1); Output
num1 = 50, *ptr1 = 50
ptr2 = ptr1; 4 /* address in ptr1 copied into ptr2 */ num2 = 10, *ptr2 = 50
21
Using Pointer Variables (within the
Same Function): Key Steps
1. Declare variables and pointer variables:
int num=20;
int *ptrI;
23
Call by Reference
• Parameter passing between functions has two modes:
– call by value [discussed in the last lecture on Functions]
– call by reference [to be discussed in this lecture]
• Call by reference: the parameter in the function holds
the address of the argument variable, i.e., the
parameter is a pointer variable. Therefore,
– In the function header’s parameter declaration list, the
parameters must be prefixed by the indirection operator *.
E.g. void distance(double *x, double *y)
– In the function call, the arguments must be pointers (or using
the address operator as the prefix).
E.g. distance(&x1, &y1);
24
Recap: Call by Value
• Call by Value – The communication between a function and
the calling body is done through arguments and the return
value of a function.
#include <stdio.h> Output
int add1(int); The value of num is: 6
27
Call by Reference: Analogy
Communications between 2 functions: Call by Reference
num ptrI
Memory
address 20 1000
1000
( *ptrI = 20 )
28
Call by Reference – Example 2
#include<stdio.h>
void function1 (int a, int *b); void function2 (int c, int *d);
void function3 (int h, int *k);
int main() {
int x, y;
x = 5; y = 5; address /* (i) */
function1(x, &y); /* (x) */
return 0;
} pointer
void function1(int a, int *b) { /* (ii) */
*b = *b + a; /* (iii) */
function2(a, b); /* (ix) */
pointer
}
void function2(int c, int *d) { /* (iv) */
*d = *d * c; /* (v) */
function3(c, d); /* (viii) */
} pointer
void function3(int h, int *k) { /* (vi) */
*k = *k - h; /* (vii) */
29
}
Call by Reference – main()
Memory
main(void) x y
{ Address = 2048
(i) x=5, y=5
int x, y; 5 5
x = 5; y = 5; 10
function1(x, &y); 50
return 0;
45
}
30
Call by Reference – function1()
Memory
main(void) x y
{ Address = 2048
int x, y; 5 5
x = 5; y = 5; 10 x=5, y=10
function1(x, &y); 50
return 0;
45
}
31
Call by Reference – function2()
Memory
main(void) x y
{ Address = 2048
int x, y; 5 5
x = 5; y = 5; 10
function1(x, &y); 50
return 0;
45
x=5, y=50
}
32
Call by Reference – function3()
Memory
main(void) x y
{ Address = 2048
int x, y; 5 5
x = 5; y = 5; 10
function1(x, &y); 50
return 0;
x=5, y=45
45
}
34
When to Use Call by Reference
When to use call by reference:
(1) When you need to pass more than one value back
from a function.
35
Double Indirection
#include <stdio.h>
int main() a
{ Memory Integer value:
int a=2; address 2 -> 3
int *p; 1024
int **pp; double indirection
p
p = &a; Memory
pp = &p; address 1024
a++; 2024
printf("a = %d, *p = %d, **pp = %d\n", a, *p, **pp);
return 0; Memorypp
} address 2024
3024
Output
a=3 Note: it could also be int ***ppp;
*p = 3 etc. The idea remains the same.
**pp = 3
36
Thank You!
37