Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sizeof(struct structname) and sizeof(object) in C

Tags:

c

sizeof

Are there scenarios where there can be a difference between sizeof(struct structure_name) and sizeof(object) where object is of type struct structure_name in C?

like image 244
himadri Avatar asked Dec 27 '22 13:12

himadri


1 Answers

No there is no difference between sizeof(type) and sizeof(o) where the declared type of o is type.

There can be differences if the declared type of the object isn't truly representative of the object. For example

char arrayValue[100];
sizeof(arrayValue);  // 100 on most systems
char* pointerValue = arrayValue;
sizeof(pointerValue);  // 4 on most 32 bit systems

This difference occurs because sizeof is a compile time construct in C. Hence there is no runtime analysis and the compiler looks instead at the statically declared types.

like image 199
JaredPar Avatar answered Feb 09 '23 01:02

JaredPar