문제

what is the difference between the below declarations?

char *argv[];

and

char *(argv[]);

I think it is same according to spiral rule.

도움이 되었습니까?

해결책

As written, the parentheses make no difference.

The so-called spiral rule falls out of this simple fact of C grammar: postfix operators such as () and [] have higher precedence than unary operators like *, so expressions like *f() and *a[] are parsed as *(f()) and *(a[]).

So given a relatively complex expression like

*(*(*foo)())[N]

it parses as

     foo            -- foo
   (*foo)           -- is a pointer (parens force grouping)
   (*foo)()         -- to a function
 (*(*foo)())        -- returning a pointer (parens force grouping again)
 (*(*foo)())[N]     -- to an array
*(*(*foo)())[N]     -- of pointer

다른 팁

Yes, they are the same. char *(argv[]) still means an array of pointers.

char (*argv)[] would be different as it means a pointer to an array of char's.

  1. argv[] is not a type so (argv[]) can't be a function declaration - it's a precedence operation.
  2. Using the spiral rule we first find [] (precedence or not) and then *, just as we do with *argv[], thus they are equal.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top