-1

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 !

  • First of all, the correct `printf` format specifier for the type `size_t` (which is the result of the `sizeof` operator) is `%zu`. Mismatching format specifier and argument type leads to *undefined behavior*. – Some programmer dude Jan 29 '22 at 10:08
  • With that said, the variable `tx` is a *pointer* so `sizeof tx` is the size of the pointer not what it points to. The variable `ty` is an array of five `T` objects, where each structure is 8 bytes (see [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member)). And the last variable `tz` is an array of five pointers. – Some programmer dude Jan 29 '22 at 10:09

1 Answers1

2

T* const tx=0, … declares tx to be a const pointer to a T, so sizeof tx is the size of a pointer to a structure.

T* const tx=0, ty[5],… declares ty to be an array of 5 T, so sizeof ty is the size of 5 such structures or 5 times the size of one such structure. Note that the C grammar structures the declaration T* const tx=0, ty[5],… as:

  • T is a declaration-specifier that here contains only a type-specifier.
  • * const tx=0 is an init-declarator-list in which * const tx is the declarator and 0 is the initializer.
  • ty[5] is another init-declarator-list that contains just the declarator ty[5] and no initializer.

Note that * const tx groups together; the * goes with const tx, not with T. So writing this as T* const tx=0, ty[5], *tz[5]; rather than t * const tx=0, ty[5], *tz[5]; portrays the grammatical grouping incorrectly. ty is an array of T, not an array of T*.

T* const tx=0, ty[5], *tz[5]; declares tz to be an array of 5 pointers to T, so sizeof tz is the size of 5 pointers to structures.

You do not need parentheses when using sizeof with options; sizeof tx suffices. And you should print sizes using the %zu conversion, not %d, so the printf statements can be:

printf("The size of tx is %zu bytes.\n", sizeof tx);
printf("The size of ty is %zu bytes.\n", sizeof ty);
printf("The size of tz is %zu bytes.\n", sizeof tz);
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312