12,890,181 members (48,126 online)
Rate this:
See more:
What does the following declaration mean?

`int (*ptr)[10];`

is above statement and below is same?
`int *ptr[10];`

Thank you.
Posted 19-Oct-12 0:30am
Updated 20-Oct-12 18:47pm
v3

Rate this:

## Solution 5

It is "declare ptr as pointer to array 10 of int". In some cases the answer is easy to get from cdecl
C gibberish ↔ English

Also, the Clockwise/Spiral rule can help to understand the syntax.
v3
Stefan_Lang 19-Oct-12 12:16pm

Sergey Chepurin 19-Oct-12 12:24pm

My pleasure)
indhukanth 21-Oct-12 0:59am

hi Sergey Chepurin is that rule applicable for all??
Sergey Chepurin 21-Oct-12 4:00am

First line of the article says - "...`Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration".
Legor 22-Oct-12 3:31am

Nelek 22-Oct-12 13:05pm

Rate this:

## Solution 6

int (*ptr)[10] - pointer to an array of integers

int* ptr[10] - an array of int pointers

Array of pointers
pointer may be arrayed like any data type. To assign the address of an integer variable called var to third element of the pointer array, write
ptr[2] = &var;

to find the value of var, write
*ptr[2].

Pointer to array
pointer to an array is a single pointer, that hold the address of an array of variables. in above case it is an integer array.

hence these two are different concept. not same
Rate this:

## Solution 1

It declares a variable `ptr` which is a pointer to an array of 10 ints
Legor 22-Oct-12 3:38am

In both of the mentioned cases?
OriginalGriff 22-Oct-12 3:45am

And the second answer is "No".
(The second is an array of pointers to ints)
Rate this:

## Solution 3

`ptr` is a pointer to an array of `10 int`.
Try
```int (*ptr)[10];
int a[10]={0};
ptr= &a;
*ptr[0] = 5;
printf("%d\n", a[0]);
```
Legor 22-Oct-12 3:38am

In both of the mentioned cases?
CPallini 22-Oct-12 6:11am

Nope: second one is the declaration of one array of 10 pointers to int.
Rate this:

## Solution 4

Hi,

as many suggests here, I don't think ptr is an array of 10 ints. Instead, it is an array of integer pointers.

ptr is an array[sized 10] of integer pointers

In fact it creates an uninitialized set of pointers. You can understand it from the code below.

```#include<stdio.h>

int main()
{
int (*ptr)[10];
*ptr[0]=1;
printf("%d", ptr[0][0]);
return 0;
}```

The aforesaid code when compiled shows a warning:
"warning: 'ptr' is used uninitialized in this function"

But when executed it outputs '1'.

If you desire, you can initialize ptr[0],ptr[1] etc with different sized integer arrays.

I think I have given enough explanations. Your comments are welcome. :-)
v2
CPallini 19-Oct-12 7:52am

What compiler are you using (the code compiled with the one I'm using right now, gcc 4.4.5, gives 'segmentation fault')?
Goutham Mohandas 19-Oct-12 8:16am

GCC version is gcc (GCC) 4.5.2
Goutham Mohandas 19-Oct-12 8:24am

Segmentation fault may rise, because ptr[0] is not initialized, therefore, the assignment writes the value on the address ptr[0] holds by default.
Stefan_Lang 19-Oct-12 8:24am

Your code doesn't prove your statement: `*ptr[0]=1;` is equivalent to `(*ptr)[0]`, and that it works just proves that ptr is a pointer to a pointer to int, same as in CPallini's code.

The warning indicates exactly the point you're missing, i. e. that you omitted the part that CPallini included, i. e. assigning a value of appropriate type.
Stefan_Lang 19-Oct-12 8:26am

Your code works because you left out the statement that would cause the compiler error - the initialization of ptr.
Goutham Mohandas 19-Oct-12 8:42am

In the solution given by Cpallini, the statement 'ptr= &a;' diverts from where the ptr was orginally pointing. Eventhough, he points the ptr to the address of another pointer a(where a is an array of ints); which clearly satisfies my statement. :-)
Stefan_Lang 19-Oct-12 8:54am

No, it satisfies *his* statement, that the type of ptr is 'pointer to array of int' (or at least convertible). *Your* statement was that ptr is of type 'array of pointer to int', and that can be easily disproven: see the comment to Solution 2.
indhukanth 21-Oct-12 1:06am

Hi All,
any one agree/disagree with Sergey Chepurin comment (Solution 5)?
plz make me clear.

Thankyou.
Legor 22-Oct-12 3:35am

Yes he is right. Solution 6 is also right and it also answers the question if the two declarations are the same (which they are not).
Legor 22-Oct-12 3:36am

Nobody here suggested that "ptr is an array of 10 ints" but they said "ptr is a pointer to an array of 10 int" which is something very different.

Top Experts
Last 24hrsThis month
 CHill60 260 OriginalGriff 240 Maciej Los 175 Jochen Arndt 175 Kornfeld Eliyahu Peter 150
 OriginalGriff 4,712 CHill60 3,288 Jochen Arndt 2,603 Karthik Bangalore 2,446 ppolymorphe 2,015