Professional Documents
Culture Documents
Pointers II
Pointers II
Pointers II
Array of Pointers
Pointer to an Array
Pointer to Multidimensional
Arrays
Pointers and Arrays
When an array is declared,
◦ The compiler allocates a base address and sufficient amount of storage to contain all the elements of the
array in contiguous memory locations.
◦ The base address is the location of the first element (index 0) of the array.
◦ The compiler also defines the array name as a constant pointer to the first element.
Contd…
• Consider the declaration:
int x[5] = {1, 2, 3, 4, 5} ;
◦ Suppose that the base address of x is 2500, and each integer requires 4 bytes.
Element Value Address
x[0] 1 2500
x[1] 2 2504
x[2] 3 2508
x[3] 4 2512
x[4] 5 2516
• Here, x &x[0] 2500 ;
◦ p = x; and p = &x[0]; are equivalent. (here p is a pointer)
◦ We can access successive values of x by using p++ or p- - to move from one element to another.
◦ Relationship between p and x:
p = &x[0] = 2500
p+1 = &x[1] = 2504
p+2 = &x[2] = 2508
p+3 = &x[3] = 2512
p+4 = &x[4] = 2516
Contd..
Sample Code
#include <stdio.h>
int main()
x X[0] X[1] X[2] X[3]
{
int x[4];
int i; Output
for(i = 0; i < 4; ++i)
{ &x[0] = 0x7ffd609e9650
printf("&x[%d] = %p\n", i, &x[i]); &x[1] = 0x7ffd609e9654
} &x[2] = 0x7ffd609e9658
printf("Address of array x: %p", x); &x[3] = 0x7ffd609e965c
return 0; Address of array x: 0x7ffd609e9650
}
From the output it can seen that the address of &x[0] and x is the same. It's because the variable
name x points to the first element of the array.
Contd…
Similarly,
&x[0] equivalent to (x) x X[0] X[1] X[2] X[3]
Sample Code
X[0] X[1] X[2] X[3] X[4]
#include <stdio.h>
int main() { x 1 2 3 4 5
int x[5] = {1, 2, 3, 4, 5};
int* ptr; ptr
ptr -1 ptr +1
ptr = &x[2];
printf("*ptr = %d \n", *ptr);
Output
printf("*(ptr+1) = %d \n", *(ptr+1));
printf("*(ptr-1) = %d", *(ptr-
*ptr = 3
1));
*(ptr+1) = 4
return 0; *(ptr-1) = 2
}
Pointers and Strings
#include <stdio.h>
int i; printf("%c\n",*p);
p++;
p=str;
p =p+1;
return 0;
printf("Next character is:%c\n",*p);
}
Array of Pointers
•There may be a situation when we want to maintain an array, which can store
pointers to an int or char or any other data type available.
p=&x;
ptr = &x[2];
p ptr
• Similarly, we can also declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful
when talking about multidimensional arrays.
• Declaration :
- data_type (*var_name)[size_of_array];
- int (*a)[10]
• Here a is pointer that can point to an array of 10 integers. Since subscript have higher precedence than indirection, it is necessary to
enclose the indirection operator and pointer name inside parentheses.
Example X[0] X[1] X[2] X[3] X[4]
Sample Code
arr 10 20 30 40 50
int main()
{
int *p; // Pointer to an integer ptr p
int (*ptr)[5]; // Pointer to an array of 5 integer
int arr[] ={10,20,30,40,50};
p = arr; // Points to 0th element of the arr
ptr = &arr; // Points to the whole array arr. Output
printf("p = %p\n",p);
printf("ptr = %p\n",ptr); p = 0x7ffe0c356550
printf("*(p+1) = %d\n",*(p+1)); ptr = 0x7ffe0c356550
printf("*(*(ptr) + 2) = %d\n",*(*(ptr) + 2)); *(p+1) = 20
printf("sizeof(*p) = %lu\n",sizeof(*p)); *(*(ptr) + 2) = 30
printf("sizeof(*ptr) = %lu\n", sizeof(*ptr)); sizeof(*p) = 4
return 0; sizeof(*ptr) = 20
}
The values in the array – accessed using the ptr *(*(ptr) + i) ), i referes to ith position in 1D array
Pointer to Multidimensional Arrays
• In a two dimensional array we can access the elements using arr[i][j] where i=row and j= column
number.
• The elements of 2-D array can be accessed with the help of pointer notation also
•Suppose arr is a 2-D array, we can access any element of the array using the pointer expression
*(*(arr + i) + j).
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
Contd…
arr points to first element of 0th 1D array
arr+1 points to first element of 1st 1D array
arr+2 points to first element of 2nd 1D array
Note
*(*(arr)) = 1 • (*(arr + i) + j) will represent the
*(*(arr+1)+1) = 6 address of jth element of ith 1-D
*(arr+2) = 9 array.
*(*(arr+2)+3) = 12 • By deferencing [*(*(arr + i) + j) ]
will give the value of jth element
of ith 1-D array.
Example
Sample Code *ptr
int main() 10 11 12 13
{
ptr
int arr[3][4] = { ptr+1 20 21 22 23
{10, 11, 12, 13}, ptr +2 30 31 32 33
{20, 21, 22, 23},
{30, 31, 32, 33}
}; *(ptr+2) *(*(ptr + 2) + 3)
int (*ptr)[4];
ptr = arr;
Output
printf("%p %p %p\n", ptr, ptr + 1, ptr + 2);
printf("%p %p %p\n", *ptr, *(ptr + 1), *(ptr + 2));
printf("%d %d %d\n", **ptr, *(*(ptr + 1) + 2), *(*(ptr + 2) + 3)); 0x7fff774eb530 0x7fff774eb540 0x7fff774eb550
printf("%d %d %d\n", ptr[0][0], ptr[1][2], ptr[2][3]); 0x7fff774eb530 0x7fff774eb540 0x7fff774eb550
return 0; 10 22 33
} 10 22 33
Question-9
What is the output ?
Sample Code Options Solution
int main()
{ A) Compiler Error
char *ptr = "GeeksQuiz"; B) G
printf("%cn", *&*&*ptr); B) G
C) Garbage value
return 0;
}
D) Runtime Error
Explanation
The operator * is used for dereferencing and the operator & is used to get the address. These operators cancel out
effect of each other when used one after another. We can apply them alternatively any no. of times. In the above code,
ptr is a pointer to first character of string ‘G’. *ptr gives us g, &*ptr gives address of g, *&*ptr again g, &*&*ptr
address of g, and finally *&*&*ptr gives ‘g’.
That is it cancels out the *& this pair continuously and final *ptr remains so, its gives ‘G’
Question-10
What is the output ?
Sample Code Options Solution
int main()
{
int arr[] = {1, 2, 3, 4, 5}; A) Compiler Error
int *p = arr; B) 2
C) 3
++*p; C) 3
p += 2;
printf("%d", *p);
D) 4
return 0;
}
Explanation
The expression ++*p is evaluated as "++(*p)" . So it increments the value of first element of array (doesn't change the
pointer p). When p += 2 is done, p is changed to point to third element of array.