Pointers in C
Pointers in C
Pointers in C
What is a pointer
int i = 5;
int *ptr; /* declare a pointer variable */
ptr = &i; /* store address-of i to ptr */
printf(“*ptr = %d\n”, *ptr); /* refer to referee of ptr */
5
Why do we need Pointer?
Remember this?
scanf(“%d”, &i);
6
What actually ptr is?
Example:
ptr address of i
int i = 5;
int *ptr;
ptr = &i; i 5
printf(“i = %d\n”, i); Output:
printf(“*ptr = %d\n”, *ptr); i = 5 value of ptr
printf(“ptr = %p\n”, ptr); = address of
*ptr = 5
i in memory
ptr = effff5e0
Pointer Operations in C
•Creation
& variable Returns variable’s memory address
•Dereference
* pointer Returns contents stored at address
•Indirect assignment
* pointer = val Stores value at address
Also have...
•Assignment
pointer = ptr Stores pointer in another variable
Twin Operators
&: Address-of operator
Get the address of an entity
• e.g. ptr = &j;
9
Twin Operators
*: De-reference operator
Refer to the content of the referee
• e.g. *ptr = 99;
10
C Pointer Variables
The declaration
int *intPtr;
defines the variable intPtr to be a pointer to a
variable of type int. intPtr will contain the
memory address of some int variable or int array.
Read this declaration as
– “intPtr is a pointer to an int”, or equivalently
– “*intPtr is an int”
The * and & operators bind more tightly than arithmetic operators so
y = *ip + 1; takes the value of the variable to which ip points, adds 1 and
assigns it to y
Similarly, the statements *ip += 1; and ++*ip; and (*ip)++; all increment
the variable to which ip points. (Note that the parenthesis are necessary
in the last statement; without them, the expression would increment ip
rather than what it points to since operators like * and ++ associate from
right to left.)
Pointer and Variable types
Note: The type of a pointer and its pointee must match
int a = 42;
int *ip;
double d = 6.34;
double *dp;
int i1;
int i2; Address: Content
int *ptr1; 0x1014 …
int *ptr2;
0x1010
i1 = 1; 0x100C …
i2 = 2;
0x1008
*ptr1 = 3;
i2 = *ptr2;
Example: Using Pointers
int i1;
int i2;
int *ptr1; 0x1014 …
int *ptr2;
0x1010 ptr2:
i1 = 1; 0x100C …
i2 = 2;
0x1008 ptr1:
*ptr1 = 3;
i2 = *ptr2;
Example: Using Pointers
int i1;
int i2;
int *ptr1; 0x1014 …
int *ptr2;
0x1010 ptr2:
i1 = 1; 0x100C …
i2 = 2;
0x1008 ptr1:
*ptr1 = 3;
i2 = *ptr2;
Example: Using Pointers
int i1;
int i2;
int *ptr1; 0x1014 …
int *ptr2;
0x1010 ptr2:
i1 = 1; 0x100C …
i2 = 2;
0x1008 ptr1: 0x1000
*ptr1 = 3;
i2 = *ptr2;
Example: Using Pointers
int i1;
int i2;
int *ptr1; 0x1014 …
int *ptr2;
0x1010 ptr2: 0x1000
i1 = 1; 0x100C …
i2 = 2;
0x1008 ptr1: 0x1000
*ptr1 = 3;
i2 = *ptr2;
Example: Using Pointers
int i1;
int i2;
int *ptr1; 0x1014 …
int *ptr2;
0x1010 ptr2: 0x1000
i1 = 1; 0x100C …
i2 = 2;
0x1008 ptr1: 0x1000
int i1;
int i2;
int *ptr1; 0x1014 …
int *ptr2;
0x1010 ptr2: 0x1000
i1 = 1; 0x100C …
i2 = 2;
0x1008 ptr1: 0x1000
*ptr1 = 3;
i2 = *ptr2;
Using Pointers (cont.)
int int1 = 1036; /* some data to point to */
int int2 = 8;
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;
What happens?
int1 becomes 8
Using Pointers (cont.)
int int1 = 1036; /* some data to point to */
int int2 = 8;
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;
What happens?
p = &a; p = &a;
p += 1; In each, p now points to b p += 1;
(Assuming compiler doesn’t
reorder variables in memory)
void
set_x_and_y(int *x,
int *y)
{ Content
*x = 1001;
*y = 1002; a 1
}
b 2
void
f(void)
{
int a = 1;
int b = 2;
set_x_and_y(&a,&b);
}
Pass-by-Reference
void
set_x_and_y(int *x,
int *y)
{ Content
*x = 1001;
*y = 1002; a 1
}
b 2
void
f(void)
{ x
int a = 1;
int b = 2; y
set_x_and_y(&a,&b);
}
Pass-by-Reference
void
set_x_and_y(int *x,
int *y)
{
*x = 1001;
*y = 1002; a 1001
1
}
b 1002
2
void
f(void)
{ x
int a = 1;
int b = 2; y
set_x_and_y(&a,&b);
}
Next Example: Pass by Reference
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i;
*ptr = -2;
An Illustration
int i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2;
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2;
pptr int ** integer pointer pointer variable
Double
Indirection
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr 5
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; Data Table
pptr = &ptr; Name Type Description Value
*ptr = 3; i int integer variable 3
**pptr = 7; j int integer variable 10
ptr = &j;
ptr int * integer pointer variable address of i
**pptr = 9;
pptr int ** integer pointer pointer variable address of ptr
*pptr = &i;
*ptr int de-reference of ptr 3
*ptr = -2;
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of 7
pptr
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 10
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of 9
pptr
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable -2
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr -2
Addresses and Pointers
Given declarations
int v;
int *p;
p = &v is a valid assignment statement.
v 5
*p (i.e., contents of p) is 5;
v 6
And it is changed to 6;
p
So v is also 6
Arrays and Pointers (in C)
Given Ai
int Ai[100]; // array of 100 ints
Ai is the starting address of the array.
Given Ai
int Ai[100];
int *pi;
pi
pi = Ai
pi = Ai +2