I have the following source :
/*file a2ptr.c */
#include <stdio.h>
#include <stddef.h>
typedef struct m_St m_St;
struct m_St
{
size_t idx;
size_t m_data[8];
}*x;
size_t GetSize(m_St *s)
{
s->idx=1;
return (sizeof((s->idx ? x : s)->m_data));
}
int main(void)
{
m_St s = { 0 };
printf("GetSize() returns: %zu\n", GetSize(&s));
return 0;
}
On a 32-bit Linux using GCC 4.8.1, this code produces the following output :
$ gcc -Wall -Werror a2ptr.c
$ ./a.out
GetSize() returns: 32
My question :
Is (s->idx ? x : s)->m_data
a valid expression in C ?
Why GetSize() returns the size of the whole array, (in bytes) and not the size of the pointer to its first element(e.g., sizeof(size_t)
) ?