Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
What does the following declaration mean?

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


Thank you.
Posted 19-Oct-12 1:30am
Edited 20-Oct-12 19:47pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
v3
Comments
Stefan_Lang at 19-Oct-12 12:16pm
   
Cool link, thanks for posting!
Sergey Chepurin at 19-Oct-12 12:24pm
   
My pleasure)
indhukanth at 21-Oct-12 0:59am
   
hi Sergey Chepurin is that rule applicable for all??
Sergey Chepurin at 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 at 22-Oct-12 3:31am
   
Nice links
Nelek at 22-Oct-12 13:05pm
   
Nice Links. +5
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It declares a variable ptr which is a pointer to an array of 10 ints
  Permalink  
Comments
Legor at 22-Oct-12 3:38am
   
In both of the mentioned cases?
OriginalGriff at 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)
Rate this: bad
good
Please Sign up or sign in to vote.

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]);
  Permalink  
Comments
Legor at 22-Oct-12 3:38am
   
In both of the mentioned cases?
CPallini at 22-Oct-12 6:11am
   
Nope: second one is the declaration of one array of 10 pointers to int.
Rate this: bad
good
Please Sign up or sign in to vote.

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. Smile | :)
  Permalink  
v2
Comments
CPallini at 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 at 19-Oct-12 8:16am
   
GCC version is gcc (GCC) 4.5.2
Goutham Mohandas at 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 at 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 at 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.
Goutham Mohandas at 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 at 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 at 21-Oct-12 1:06am
   
Hi All,
any one agree/disagree with Sergey Chepurin comment (Solution 5)?
plz make me clear.

Thankyou.
Legor at 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 at 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.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,434
1 OriginalGriff 6,033
2 Peter Leow 2,534
3 Maciej Los 2,268
4 Abhinav S 2,264


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100