разница между int bar [10] и int (* bar) [10] - PullRequest
4 голосов
/ 24 сентября 2011
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] совершенно другой.

Спасибо Раджа

Ответы [ 5 ]

9 голосов
/ 24 сентября 2011

Они совершенно разные.Первый - это массив.Второй - указатель на массив.

Ваш комментарий после первого объявления bar абсолютно неверен.Первый bar - это массив из 10 int с.Период.Это не указатель (т. Е. Ваша часть «что означает» вообще не имеет смысла).

Это можно выразить так:

typedef int T[10];

Ваш первый bar имеет тип T, а у вас второй bar имеет тип T *.Вы понимаете разницу между T и T *, не так ли?

3 голосов
/ 24 сентября 2011

Вы можете сделать это:

int a[10];

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

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

Но вы не можете сделать это:

int a[10];

int bar[10] = a;  // Compiler error!  Can't assign one array to another
1 голос
/ 24 сентября 2011

Эти два объявления не объявляют один и тот же тип.

Ваше первое объявление объявляет массив int.

Ваше второе объявление объявляет указатель на массив int.

0 голосов
/ 24 сентября 2011

Там также cdecl(1) и 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 голосов
/ 24 сентября 2011

Вот ссылка о C "правое левое правило", которое я нашел полезным при чтении сложных объявлений c: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html. Это также может помочь вам понять разницу между int bar[10] и int (*bar)[10].

Взято из артикула:

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...