Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

When I tried to use the sizeof operator to determine the exact size of some objects, I got zero as the result. What is wrong?

0
Posted

When I tried to use the sizeof operator to determine the exact size of some objects, I got zero as the result. What is wrong?

0

You probably tried something like printf (“%d”, sizeof (something)); The ANSI standard proposes that the sizeof operator returns a value of type size_t, which is in fact long integer in this implementation. So, the result is pushed on the stack as a long integer, but the format specifier “%d” expects an ordinary integer, so it pulls from the stack just one word, which is zero in this case. You need to write printf (“%ld”, sizeof (something)); Alternatively, you can use a typecast to convert the result to a short integer printf (“%d”, (short) sizeof (something)); assuming that no object would be longer that 32767 bytes.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123