Whilst performing a 'typedef int triple[3]', I get results that are confusing to me. The type of the stack variable is indeed an int *[3]. However, 's' is not an int[3], but an int *[3] also. Furthermore, when dereferencing stack[5], the resulting lvalue is an int[3], as I would expect, but when assigning this to 's', I get the pointer to this triple, presumably because s is defined as an int *[3], not an int[3].
My questions are: why is 's' defined as an int *[3], instead of an int[3] and why does assigning an int[3] to 's' result in the assignment of the address of this particular int[3]?
The code has been compiled with gcc-4.1.2.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef int triple[3];
int
main()
{
triple *stack;
triple s;
int i, j;
printf("sizeof(stack): %i\n", sizeof(stack));
stack = (triple *) malloc(10*sizeof(stack));
if(stack == NULL)
return 1;
printf("sizeof(stack) after malloc: %i\n", sizeof(stack));
for(i = 0; i < 10; i++)
for(j = 0; j < 3; j++)
stack[i][j] = i*3 + j;
s = stack[5];
printf("s[0], s[1], s[2]: %i, %i, %i\n", s[0], s[1], s[2]);
return 0;
}