Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What does the following declaration mean?

C++
int (*ptr)[10];

and One More Please..
is above statement and below is same?
C++
int *ptr[10];



Thank you.
Posted
Updated 20-Oct-12 18:47pm
v3

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.
 
Share this answer
 
v3
Comments
Stefan_Lang 19-Oct-12 12:16pm    
Cool link, thanks for posting!
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    
Nice links
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
 
Share this answer
 
It declares a variable ptr which is a pointer to an array of 10 ints
 
Share this answer
 
Comments
Legor 22-Oct-12 3:38am    
In both of the mentioned cases?
OriginalGriff 22-Oct-12 3:45am    
Second one added since answers given, if you check the dates.
And the second answer is "No".
(The second is an array of pointers to ints)
ptr is a pointer to an array of 10 int.
Try
C
int (*ptr)[10];
int a[10]={0};
ptr= &a;
*ptr[0] = 5;
printf("%d\n", a[0]);
 
Share this answer
 
Comments
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.
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.

C++
#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. :-)
 
Share this answer
 
v2
Comments
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:26am    
Your code works because you left out the statement that would cause the compiler error - the initialization of ptr.
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900