-1

Assume that you have the struct my_struct in C. Then you are using sizeof(my_struct) to find how many bytes that struct have. Is there any way to find out that my_struct is a struct just by using sizeof() method in C?

For example, if I want to find out that my_variable is 16-bit, then I just check if sizeof(my_variable) == 2.

Can I do the same for struct in C?

euraad
  • 2,467
  • 5
  • 30
  • 51
  • 1
    Certainly not, because it's possible to have a struct whose size is, say, 2. – Steve Summit Nov 01 '21 at 17:44
  • 4
    No. Why would you even want to do that? It is your code and you know what the type is. – Eugene Sh. Nov 01 '21 at 17:44
  • 3
    If such an introspection feature did exist, `sizeof` wouldn't be the place for it (How is "is this a struct?" related to the question of "how big is this thing in bytes?"). I'm not aware of such a language feature. But I'm curious, *if it did exist*, what would you use it for? – Alexander Nov 01 '21 at 17:47
  • 1
    Checking that your variable has a size of 2 and is "thus" 16 bits, still doesn't mean anything in C. Is it a short, an unsigned short, a (very short) string, a struct of 2 chars? – 9769953 Nov 01 '21 at 17:50
  • @Alexander In, say, Python, you can use check whether a value is an integer or float, and pass it through a different control flow depending on the result. Although I can't really think of a real good reason for that anyway. Perhaps the OP is thinking along those lines. – 9769953 Nov 01 '21 at 17:52
  • @9769953 Even in Python, where that's *possible*, doing explicit type-checks like is very suspicious (and discouraged), because it totally undermines the whole "point" of object orientation (polymorphism and substitutability). "Although I can't really think of a real good reason for that anyway." Precisely, that's why I think it's so important to understand what OP is trying to achieve with this feature, if it hypothetically existed. – Alexander Nov 01 '21 at 18:17
  • I may be over-reading the intention of the question, but if the goal is to take a particular action based on the type of a variable, you may want to look into the `_Generic` keyword added in C11. There's some details [here](https://stackoverflow.com/questions/9804371/syntax-and-sample-usage-of-generic-in-c11) – sj95126 Nov 01 '21 at 18:45

1 Answers1

4

Can I do the same for struct in C?

sizeof(my_variable) does the exact same thing for structs as for non-structs.

Specifically,

  • sizeof( my_variable ) == 2 doesn't mean the variable uses up 16 bit.

    It means that my_variable uses up 2 bytes. Even though a byte is usually 8 bits in size these days, that's not always the case. Historically, they have been as large as 32 bits!

  • sizeof( my_variable ) == 2 doesn't mean the variable can store 16-bit numbers.

    sizeof doesn't say anything about the type except for its size.

For example,

sizeof( long ) sizeof( float ) and sizeof( struct { char a; int b; } ) could all return the same number.

ikegami
  • 367,544
  • 15
  • 269
  • 518