Datatype * <array-name>[array-size];
int *ptr[#];
1) An array of # integer pointers
2) Generally this can be used for pointing to array of data items.
How it was named using operator precedence ?
Regular rule for deciphering in C is right-left rule. You can learn right-left rule Click here.
If you want to interpret an expression, use this site Click here
Declaration
An array of 3 integer pointers
points to remember
- ptr is just base address of ptr[0]
- In the above example, we have 3 pointers ptr[0], ptr[1], ptr[2]
- *ptr[0] is dereference of ptr[0]
Initialization
Since it is array of pointers, we need to assign an address for every pointer.
int *ptr[3];
Static Allocation
int arr[3] = {6,7,8};ptr[0] = &arr[0];
Array of Pointers |
Dynamic Allocation
int i;for (i=0;i<3;i++){
ptr[i] = (int *)malloc(sizeof(int));
if (ptr[i] != NULL)
*ptr[i] = i;
}
How to access an array of pointers?
One way to access, use array of pointer
printf ("value of *ptr[0] using %%d %d\n",*ptr[0]);
printf ("value %d\n",*ptr[1]);
printf ("value %d\n",*ptr[2]);
Output
value of *ptr[0] using %d 6
value of 7
value of 8
Another way to access, use 2D array
printf ("value of ptr[0][0] %d\n",ptr[0][0]);
Output
value of ptr[0][0] 6
value of ptr[1][0] 7
One more way is , use pointer incrementation
printf ("value of **ptr %d\n",**ptr);
printf ("value of **(ptr + 1) %d\n",**(ptr+1));
printf ("value of **(ptr + 2) %d\n\n",**(ptr+2));
Output
value of **ptr 6
value of **(ptr + 1) 7
value of **(ptr + 2) 8
Array of pointers - Clarification
Eg
int *ptr[3];
int arr[3] = {6,7,8};
ptr[0] = &arr[0];
ptr[1] = &arr[1];
ptr[2] = &arr[2];
printf ("value ptr using %%x %x\n",ptr);
printf ("value of &ptr using %%x %x\n",&ptr);
printf ("value of ptr using %%d %d\n\n",ptr);
printf ("value of ptr[0] using %%x %x\n",ptr[0]);
printf ("value of &(ptr[0]) using %%x %x\n",&(ptr[0]));
printf ("value of *ptr[0] using %%d %d\n",*ptr[0]);
printf ("value of &(*ptr[0])using %%x %x\n\n",&(*ptr[0]));
printf ("Value of ptr[1] using %%x %x\n",ptr[1]);
printf ("value of &ptr[1] using %%x %x\n",&(ptr[1]));
printf ("value of *ptr[1] using %%d %d\n",*ptr[1]);
printf ("value of &(*ptr[1]) using %%x %x\n\n",&(*ptr[1]));
printf ("size of ptr %d\n",sizeof(ptr));
printf ("size of ptr[0] %d\n",sizeof(ptr[0]));
printf ("size of ptr[1] %d\n",sizeof(ptr[1]));
printf ("size of ptr %d\n\n",sizeof(*ptr[3]));
printf ("value of ptr[0][0] %d\n",ptr[0][0]);
printf ("value of ptr[0][1] %d\n",ptr[0][1]);
printf ("value of ptr[0][2] %d\n",ptr[0][2]);
printf ("value of ptr[0][3] %d\n",ptr[0][3]);
printf ("value of ptr[1][0] %d\n",ptr[1][0]);
printf ("value of ptr[1][1] %d\n\n",ptr[1][1]);
printf ("value of **ptr %d\n",**ptr);
printf ("value of *(ptr + 1) using %%x %x\n",*(ptr+1));
printf ("value of **(ptr + 1) %d\n",**(ptr+1));
printf ("value of **(ptr + 2) %d\n\n",**(ptr+2));
printf ("value of %d\n",*ptr[1]);
printf ("value of %d\n",*ptr[2]);
Output
value ptr using %x dd9bacc0
value of &ptr using %x dd9bacc0
value of ptr using %d -577000256
value of ptr[0] using %x dd9bace0
value of &(ptr[0]) using %x dd9bacc0
value of *ptr[0] using %d 6
value of &(*ptr[0])using %x dd9bace0
Value of ptr[1] using %x dd9bace4
value of &ptr[1] using %x dd9bacc8
value of *ptr[1] using %d 7
value of &(*ptr[1]) using %x dd9bace4
size of ptr 24
size of ptr[0] 8
size of ptr[1] 8
size of ptr 4
value of ptr[0][0] 6
value of ptr[0][1] 7
value of ptr[0][2] 8
value of ptr[0][3] 0
value of ptr[1][0] 7
value of ptr[1][1] 8
value of **ptr 6
value of *(ptr + 1) using %x dd9bace4
value of **(ptr + 1) 7
value of **(ptr + 2) 8
value of 7
value of 8
Kindly check the image above to get clarified..!
Advantages
This is useful in sorting the array of character pointers by reordering the pointer array without moving the data items<strings>. Reordering the pointer array is fast compared to reordering the data items or strings.Trial and Error
1)
int *ptr[3]; // Correct Declaration
int *ptr[3] = {6,7,8}; //Incorrect Declaration & Initialization
Compilation Error
arr_of_ptrs.c: In function ‘main’:
arr_of_ptrs.c:7: error: redeclaration of ‘ptr’ with no linkage
arr_of_ptrs.c:5: note: previous declaration of ‘ptr’ was here
arr_of_ptrs.c:7: warning: initialization makes pointer from integer without a cast
arr_of_ptrs.c:7: warning: initialization makes pointer from integer without a cast
arr_of_ptrs.c:7: warning: initialization makes pointer from integer without a cast
Reason
Without allocating memory, we can't directly assign value to the pointer.
2)
int *ptr[3]; // Correct Declaration
*ptr[0] = 15; /*Can't be initialized directly - leads to segmentation fault*/
No compilation errors, it thinks memory is allocated already.
While executing, leads to segmentation fault. Because memory is not allocated.
3)
int arr[3] = {6,7,8};
*ptr[0] = arr[0]; // Can't be initialized like this - leads to segmentation fault
No compilation errors
It needs an address, instead of a value as same as 2)
4)
ptr[0] = arr[0];
It gives compilation warning
warning: assignment makes pointer from integer without a cast
5)
ptr = arr[0];
error: incompatible types when assigning to type ‘int *[3]’ from type ‘int’
Reason
ptr is not a pointer or value, rather just name here.
6)
ptr = &arr[0];
error: incompatible types when assigning to type ‘int *[3]’ from type ‘int *’
Reason
ptr is not a pointer or value, rather just name here.
7)
ptr = &arr;
error: incompatible types when assigning to type ‘int *[3]’ from type ‘int (*)[3]'
Reason
ptr is not a pointer or value, rather just name here.
8)
ptr = arr;
error: incompatible types when assigning to type ‘int *[3]’ from type ‘int (*)[3]'
Reason
ptr is not a pointer or value, rather just name here.
9)
printf ("value *(ptr + 1)%d\n",*(ptr+1));
value *(ptr + 1)1414866912
Reason
Ptr is address of ptr[0], ptr+1 is address of ptr[1], *(ptr+1) is the value of ptr[1] holds.
If you want to get the value of ptr[1] points to, you should use **(ptr+1).
10)
const char *args[];
args[0] has value 2
Some trial and error scenarios
printf("%d\n", args[0]);
printf("%s\n",*args[0]);
printf("%s\n", args[0]);
printf("%d",*args[0]);
if (args[0] != value)
-1093622160
Segmentation Fault
2
51
error: comparison between pointer and integer [-Werror]
Useful Sites
stackoverflow.com
http://ee.hawaii.edu/~tep/EE160/Book/chap9/section2.1.4.html
Comments
Post a Comment