0% found this document useful (0 votes)
22 views37 pages

4 - Pointers - Lecture (Slides)

Pointers c

Uploaded by

steph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views37 pages

4 - Pointers - Lecture (Slides)

Pointers c

Uploaded by

steph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4

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

– Primitive Data Types, Variables and Address


Operator
– Pointer Variables
– Call by Reference

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

num num stores the value.


num = 5,
5
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
1003
1004
1005
1006
1007
1008
1009

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

num num stores the value.


num = 5,
10
1000 0 1 0 1 0 1 1 0
1001 Content of
1 0 1 0 0 1 0 1
memory
10 1002
1003
0 1 0 1 1 0 1 0

num = 10, 1004


1005
1006
1007
1008
1009

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

num = 10, &num = 1000 1005


1006
1007
1008
1009

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.

Note: You may also print the address of the


variable using the printf() statement.
7
Pointers

– Primitive Data Types, Variables and Address


Operator
– Pointer Variables
– Call by Reference

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:

int *ptrI; Address


Memory
ptrI
1000

addr
0 1 0 1 0 1 1 0

or int * ptrI; 1001


1002
1
0
0
1
1
0
0
1
0 1
1 0
0
1
1
0
Content of
memory
1003

or int* ptrI; 1004


1005
1006
1007
1008 10 ptrI – 4 bytes of
1009
memory allocated
• ptrI is a pointer variable. It does not store
the value of the variable. It stores the
address of the memory which is used for
storing an Int value.
9
Pointer Variables: Analogy
ptrI 1006
Memory Integer
address Value Stored
1006
(4 bytes)
• Analogy:
(1) Address on envelope  your home

(2) Bank account  your saving/money in the bank

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.

ptrC Memory character value


3024
address stored (1 byte)
3024

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

pointer variable ptrI. 1006


1007 20
• The value is referred to by using the 1008
1009

indirection operator (*), i.e. *ptrI.


• For example: we can assign
*ptrl = 20;
=> the value 20 is stored at the address
pointed to by ptrI.
12
a
How to use Pointer Addr:1000 20
Variables? b
Addr:2000 40.0
• Declare variables c
int a=20; float b=40.0; char c='a'; Addr:3000 a
int *ptrI; float *ptrF; char *ptrC;
ptrI
• After declaration, memories Addr:4000 ?
will be allocated for each ptrF
primitive variable according to Addr:5000 ?
its data type. ptrC
• For each pointer variable, 4 Addr:6000 ?
bytes of memory will be
allocated.

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 = &num; 1024 3 Address = 1024
int num = 3; // integer var ptr num
int * ptr; // pointer var *ptr = 10; 1024 10 Address = 1024

ptr = &num; // assignment Output


num = 3, &num = 1024
// Question: what will be ptr, *ptr, num?
ptr = 1024, *ptr = 3
printf("num = %d, &num = %p\n", num, &num);

printf("ptr = %p, *ptr = %d\n", ptr, *ptr);

}
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 = &num; 1024 3 Address = 1024
int num = 3; // integer var ptr num
int *ptr; // pointer var *ptr = 10; 1024 10 Address = 1024

ptr = &num; Output


num = 3, &num = 1024
printf("num = %d, &num = %p\n", num, &num); ptr = 1024, *ptr = 3

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

ptr1 = &num1; /* put the address of num1 into ptr1 */


// What are the values for num1, *ptr1? Output
num1 = 3, *ptr1 = 3
printf(“num1 = %d, *ptr1 = %d\n”, num1, *ptr1);
num2 = 5, *ptr2 = 5

ptr2 = &num2; /* put the address of num2 into ptr2 */


// What are the values for num2, *ptr2?
printf(“num2 = %d, *ptr2 = %d\n”, num2, *ptr2);

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*/

*ptr2 = *ptr1; ptrl ptr2


1024 2048
// What are the values for num2, *ptr2?
printf(“num2 = %d,*ptr2 = %d\n”,num2, *ptr2);
Output

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

printf(“num2 = %d, *ptr2 = %d\n”, num2, *ptr2);


return 0;
}

21
Using Pointer Variables (within the
Same Function): Key Steps
1. Declare variables and pointer variables:
int num=20;
int *ptrI;

2. Assign the address of variable to pointer variable:


ptrI=&num; (*ptrI)
num
ptrI 1000 Memory
address 20
1000

Then you can retrieve the value of the


variable num through *ptr as well.
22
Pointers

– Primitive Data Types, Variables and Address


Operator
– Pointer Variables
– Call by Reference

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

int main( ) num 5 -> 6


{
int num = 5;
num = add1(num); // num – called argument
printf("The value of num is: %d", num);
return 0;
}
int add1(int value) // value – called parameter
{
value++; value 5 -> 6
return value;
}
25
Call by Reference: Example 1
#include <stdio.h>
void add2(int *ptr);
Output
int main() Value of num is 6
{ Memory
int num = 5; main(void)
/*passing the address of num*/ { num
add2(&num); int num = 5; 1 Address = 1024
add2(&num);
&num 2 5 6
printf("Value of num is: %d",
……
num); } 3
return 0;
}
4
void add2(int *ptr) void add2(int *ptr)
ptr ptr
{ {
++(*ptr);
++(*ptr) 1024
++(*ptr);
}
}

• Any change to the value pointed to by the


parameter ptr will change the argument value
num (instantly).
26
Call by Reference: Key Steps
1. In the function definition, the parameter must be
prefixed by indirection operator *:

add2( ): void add2( int *ptr ) { …}

2. In the calling function, the arguments must be


pointers (or using address operator as the prefix):

main( ): int num; add2( &num ) ;

27
Call by Reference: Analogy
Communications between 2 functions: Call by Reference

(1) main(): int num; add2(&num ) ; (2) void add2(int *ptr){…}


num ptr
Memory 5
address
1024
1024 (*ptr = 5)
Analogy: using pointer within a function:
(1) int num; int *ptrI; (2)ptrI = &num;

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
}

void function1(int a, int *b)


{ a b
5 + 5
*b = *b + a; 5 2048
function2(a, b);
}

void function2(int c, int *d)


{ c d
10 * 5
*d = *d * c; 5 2048
function3(c, d);
}

void function3(int h, int *k)


{ h k
50 - 5
*k = *k - h; 5 2048
}

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
}

void function1(int a, int *b)


{
5+5 a b (ii) a=5, *b=5
*b = *b + a; 5 2048
function2(a, b);
(iii) a=5, *b=5+5=10
}

void function2(int c, int *d)


{ c d
10 * 5
*d = *d * c; 5 2048
function3(c, d);
}

void function3(int h, int *k)


{ h k
50 - 5
*k = *k - h; 5 2048
}

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
}

void function1(int a, int *b)


{ a b
5 + 5
*b = *b + a; 5 2048
function2(a, b); a=5, *b=50
}

void function2(int c, int *d)


{
10 * 5 c d (iv) c=5, *d=10
*d = *d * c; 5 2048
function3(c, d); (v) c=5, *d=10*5=50
}

void function3(int h, int *k)


{ h k
50 - 5
*k = *k - h; 5 2048
}

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
}

void function1(int a, int *b)


{ a b
5 + 5
*b = *b + a; 5 2048 a=5, *b=45
function2(a, b);
}

void function2(int c, int *d)


{ c d
10 * 5
*d = *d * c; 5 2048
function3(c, d); c=5, *d=45
}

void function3(int h, int *k)


{ h k
50 - 5
*k = *k - h; 5 2048
}
(vi) h=5, *k=50
33 (vii) h=5, *k=50-5=45
Call by Reference – Example 2
x y a *b c *d h *k remarks
(i) 5 5 - - - - - - in main
(ii) 5 5 5 5 - - - - in fn 1
(iii) 5 10 5 10 - - - - in fn 1
(iv) 5 10 5 10 5 10 - - in fn 2
(v) 5 50 5 50 5 50 - - in fn 2
(vi) 5 50 5 50 5 50 5 50 in fn 3
(vii) 5 45 5 45 5 45 5 45 in fn 3
(viii) 5 45 5 45 5 45 - - return to
fn 2
(ix) 5 45 5 45 - - - - return to
fn 1
(x) 5 45 - - - - - - return to
main

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.

(2) When using call by value will result in a large piece


of information being copied to the formal
parameter, for efficiency reason, for example,
passing large arrays or structure records.

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

You might also like