Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between *ptr[10] and (*ptr)[10]

Tags:

c

pointers

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.

like image 412
Amit Gupta Avatar asked Dec 17 '12 08:12

Amit Gupta


People also ask

What is the difference between int * A 10 and int (* A )[ 10?

int *a[10] is an array of integer pointers. int (*a)[10] is pointer to an array of integers.

What is the meaning of (* ptr )*= 10?

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

What is the meaning of the following declaration in C language int (* d )[ 10 ];?

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.

What is the meaning of int (* a 3?

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.


5 Answers

int *ptr[10];

This is an array of 10 int* pointers, not as you would assume, a pointer to an array of 10 ints

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 ints

like image 185
Karthik T Avatar answered Sep 23 '22 23:09

Karthik T


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.

like image 38
P.P Avatar answered Sep 23 '22 23:09

P.P


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.

like image 26
Sree harsha Punyasamudram Avatar answered Sep 23 '22 23:09

Sree harsha Punyasamudram


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;
}
like image 26
Jeyaram Avatar answered Sep 21 '22 23:09

Jeyaram


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 .

like image 34
wanttomasterpython Avatar answered Sep 22 '22 23:09

wanttomasterpython