I need help solving this problem in my mind so if anyone had a similar problem it would help me a lot.
Problem is:
Let the type be defined:
typedef struct t
{
char c; char *p;
} T;
What will the following code print?
T* const tx=0, ty[5], *tz[5];
printf("tx:%d\n", sizeof(tx));
printf("ty:%d\n", sizeof(ty));
printf("tz:%d\n", sizeof(tz));
The program prints:
tx=4
ty=40
tz=20
What interests me is why is the print like this?
The first variable of type tx
is of the type of structure T
, which indicates a constant value which is zero.
As far as I understand, it is necessary to do a sizeof of the value indicated by the variable tx, it depends on the machine I use, so it is 4.
Also for the third printout it is necessary to do a sizeof of the value pointed by tz
, since on my machine the sizeof of the pointer is equal to 4, here we have 5 elements of the array so it is equal to 20.
For the second print I have no idea why it prints 40?
Is my thinking correct for the first and third print?
If anyone is willing to explain to me why the second printout is 40, I would be grateful?
Thanks in advance !
Best regards !