For the following code:
int (*ptr)[10];
int a[10]={99,1,2,3,4,5,6,7,8,9};
ptr=&a;
printf("%d",(*ptr)[1]);
What should it print? I'm expecting the garbage value here but the output is 1
.
(for which I'm concluding that initializing this way pointer array i.e ptr[10]
would start pointing to elements of a[10]
in order).
But what about this code fragment:
int *ptr[10];
int a[10]={0,1,2,3,4,5,6,7,8,9};
*ptr=a;
printf("%d",*ptr[1]);
It is giving the segmentation fault.
int *a[10] is an array of integer pointers. int (*a)[10] is pointer to an array of integers.
*ptr[10] means 'ptr is an array of pointer type 10 elements'. (*ptr)[10] means there is an array of 10 elements with no array varaible but 'ptr is pointer type variable' that has base address of that array.
int (*ptr)[10]; What does the following declaration mean? int (*ptr)[10];1) ptr is array of pointers to 10 integers 2)ptr is a pointer to an array of 10 integers 3)ptr is an array of 10 integers 4)ptr is an pointer to array.
The int *(a[3]) is the same as plain int *a[3] . The braces are redundant. It is an array of 3 pointers to int and you said you know what it means. The int (*a)[3] is a pointer to an array of 3 int (i.e. a pointer to int[3] type). The braces in this case are important.
int *ptr[10];
This is an array of 10 int*
pointers, not as you would assume, a pointer to an array of 10 int
s
int (*ptr)[10];
This is a pointer to an array of 10 int
It is I believe the same as int *ptr;
in that both can point to an array, but the given form can ONLY point to an array of 10 int
s
int (*ptr)[10];
is a pointer to an array of 10 ints.
int *ptr[10];
is an array of 10 pointers.
Reason for segfault:
*ptr=a; printf("%d",*ptr[1]);
Here you are assigning the address of array a
to ptr
which would point to the element a[0]
. This is equivalent to: *ptr=&a[0];
However, when you print, you access ptr[1]
which is an uninitialized pointer which is undefined behaviour and thus giving segfault.
int (*p)[10]
is a pointer to an array of 10 integers in each row i.e there can be any number of rows. basically it can be used to point to a 2D array and the dimensions can be accessed by incrementing i
for *(p+i)[10]
which is same as a[i][10]
, here 'i=1,2,3...'
to access 1,2,3.. rows
.
where as, int *p[10]
is an array of 10 integer pointers.
If array is two dimesional i.e, for example
b[2][10]={{0,1,2,3,4,5,6,7,8,9},{10,11,12,13,14,15,16,17,18,19}};
basically,
(*ptr)[10] // where the higher dimension index is omitted i.e, 2
can be used as two dimensional pointer to an array(containing 10 elements in each row i.e, the 1st dimension) like (*ptr)[10] = &b
.
In the printf("%d",(*ptr)[1]);
as provided (*ptr)
is same as *(ptr+0)
evaluates to the first dimension i.e, b[0][0]
which value is 0. like wise, to access the 2nd dimension of the array b[1][0]
the expression will be **(ptr+1)
or *(ptr+1)[0]
or *(*(ptr+i)+j);
// where i=1 and j=0
gives the first element of the 2nd dimension i.e, 10.
I've answered it long so to understand it easy.
int(*)[10]
is a pointer to an int array with 10 members. i.e it points to int a[10]
.
where as int *[10]
is array of integer pointers
#include <stdio.h>
int main()
{
int *ptr[10];
int a[10]={0,1,2,3,4,5,6,7,8,9};
printf("\n%p %p", ptr[0], a);
*ptr=a; //ptr[0] is assigned with address of array a.
printf("\n%p %p", ptr[0], a); //gives you same address
printf("\n%d",*ptr[0]); //Prints zero. If *ptr[1] is given then *(ptr + 1) i.e ptr[1] is considered which is uninitialized one.
return 0;
}
int (*p)[10] means that p is now a pointer to an integer array of size 10.
int *p[10] means that p is an array of 10 integer pointers .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With