开发者

difference between int bar[10] vs int (*bar)[10]

开发者 https://www.devze.com 2023-04-07 19:28 出处:网络
int bar[10]; /* bar is array 10 of int, which means bar is a pointer to array 10 of int */ int (*bar)[10]; /* bar is a pointer to array 10 of int */
int bar[10]; /* bar is array 10 of int, which means bar is a pointer to array 10 of int */

int (*bar)[10]; /* bar is a pointer to array 10 of int */

According to me th开发者_Python百科ey both are same, am I wrong? Please tell me.

Edit: int *bar[10] is completely different.

Thanks Raja


They are completely different. The first one is an array. The second one is a pointer to an array.

The comment you have after the first bar declaration is absolutely incorrect. The first bar is an array of 10 ints. Period. It is not a pointer (i.e. your "which means" part makes no sense at all).

It can be expressed this way:

typedef int T[10];

Your first bar has type T, while you r second bar has type T *. You understand the difference between T and T *, do you?


You can do this:

int a[10];

int (*bar)[10] = &a;  // bar now holds the address of a

(*bar)[0] = 5;  // Set the first element of a

But you can't do this:

int a[10];

int bar[10] = a;  // Compiler error!  Can't assign one array to another


These two declarations do not declare the same type.

Your first declaration declares an array of int.

Your second declaration declares a pointer to an array of int.


Here is a link about the C "right-left rule" I found useful when reading complex c declarations: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html. It may also help you understand the difference between int bar[10] and int (*bar)[10].

Taken from the article:

First, symbols.  Read

     *      as "pointer to"         - always on the left side
     []     as "array of"           - always on the right side
     ()     as "function returning" - always on the right side

as you encounter them in the declaration.

STEP 1
------
Find the identifier.  This is your starting point.  Then say to yourself,
"identifier is."  You've started your declaration.

STEP 2
------
Look at the symbols on the right of the identifier.  If, say, you find "()"
there, then you know that this is the declaration for a function.  So you
would then have "identifier is function returning".  Or if you found a 
"[]" there, you would say "identifier is array of".  Continue right until
you run out of symbols *OR* hit a *right* parenthesis ")".  (If you hit a 
left parenthesis, that's the beginning of a () symbol, even if there
is stuff in between the parentheses.  More on that below.)

STEP 3
------
Look at the symbols to the left of the identifier.  If it is not one of our
symbols above (say, something like "int"), just say it.  Otherwise, translate
it into English using that table above.  Keep going left until you run out of
symbols *OR* hit a *left* parenthesis "(".  

Now repeat steps 2 and 3 until you've formed your declaration.


There's also cdecl(1) and http://cdecl.org/

$ cdecl explain 'int bar[10]'
declare bar as array 10 of int

$ cdecl explain 'int (*bar)[10]'
declare bar as pointer to array 10 of int
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号