1

To be more specific, I'm struggling to comprehend why this fails in compilation:

 int array[] = {4, 3, 2, 1};
 typeof(array) __array_copy = (array);

with the error being:

main.c: In function 'test_map':
main.c:113: error: invalid initializer

However this works:

 int array[] = {4, 3, 2, 1};
 typeof(*array) *__array_copy = (array);

What exactly is the difference between typeof(array) vs typeof(*array) *?

R.D.
  • 2,471
  • 2
  • 15
  • 21
  • 2
    The former is an array, the latter is a pointer. You can't initialize an array with another array (for some reason, the C standard committee thought that it's a good idea, I think it's not). You can, however, initialize a pointer with a pointer, which is formed by the array decaying into a pointer to its first element. And before you ask it: **No, an array is not just a pointer.** (and `__array_copy` is **not** a copy of the original array. It isn't even correct code -- identifiers starting with a double underscore are reserved for the implementation, so this code has undefined behavior.) – The Paramagnetic Croissant Jul 03 '14 at 19:19
  • possible duplicate of [Pointer vs array in C, non-trivial difference](http://stackoverflow.com/questions/660752/pointer-vs-array-in-c-non-trivial-difference) – The Paramagnetic Croissant Jul 03 '14 at 19:21
  • @user3477950, thanks for the answer, I think that explains it adequately. Mind reposting it as an "answer" rather than a comment so I can accept it? – R.D. Jul 03 '14 at 19:24
  • Since this is actually a duplicate, I'd rather not post a separate answer. – The Paramagnetic Croissant Jul 03 '14 at 19:25
  • @SteveCox it's a GNU extension to C. – The Paramagnetic Croissant Jul 03 '14 at 20:07
  • @SteveCox, It's a GCC extension https://gcc.gnu.org/onlinedocs/gcc/Typeof.html – R.D. Jul 03 '14 at 20:08
  • then please mark this gcc, not just C – Steve Cox Jul 03 '14 at 20:17

0 Answers0