What arguments does the sizeof operator take in C?
[The original name refers to the sizeof function.]
I tried these and they all worked:
char * p; printf ("Size of * p is% d \ n", sizeof (* p)); // result = 1 printf ("Size of p is% d \ n", sizeof (p)); // result = 4 printf ("Size of p is% d \ n", sizeof (& p)); // result = 4
I wonder why the first printf is 1, the 2nd and 3rd are 4? So what arguments can sizeof have?
source to share
Type required.
sizeof(char)
always alone. The variable p
itself is a pointer, and on your platform is of size 4. Then you execute &p
or a pointer to a pointer, which is also of size 4.
On most modern desktop systems, a 32-bit architecture will contain 4 byte pointers, and a 64-bit architecture will contain 8 byte pointers.
sizeof
itself is a compile-time allowed keyword, not a function. In C99, arrays can be of variable length, and sizeof will wait at runtime to allow that size.
source to share
sizeof
is not a function; this is the operator. It can be used in two ways: how sizeof(typename)
and how sizeof expression
. When used with a type name, parentheses are required. The parentheses are not needed when the operand is an expression, although for clarity, many programmers will express the expression in parentheses independently. Note that unlike most programming language operators, it sizeof expression
does not evaluate its argument under normal circumstances, that is, when its operand is not a C99 variable length array.
source to share
The first answer says char is 1 byte. (derefing your pointer to the actual char data)
The second says that the pointer value is a 4-byte value, that is, the memory location is represented by a 4-byte value.
The third says that the pointer address is 4 bytes.
Size can take all of these arguments, and it provides the exact answer for all values.
Greetings.
source to share
In addition to other answers, beware of possible terminological confusion.
In the C world, "byte" is the same as "char". That is, each 'char' is 1 'byte' by definition. This byte value is not necessarily related to the machine byte in the underlying hardware. C 'byte' (i.e. 'char') can be multiple bytes of the machine. However, in a C program, you don't have access to machine bytes. Everything is measured in C bytes (ie "characters"), and "sizeof" gives the size, measured in "characters".
For these reasons, 'sizeof (char)' is always 1, no matter how many machine bytes each 'char' consists of.
source to share
The operand of the operator sizeof
is the type.
If you use sizeof with an object or value, its type is used instead.
sizeof 65; /* same as `sizeof (int);` */
sizeof 'X'; /* same as `sizeof (int);` */
sizeof 42.0; /* same as `sizeof (double);` */
float f;
sizeof f; /* same as `sizeof (float);` */
/* etc ... */
You can specify the type directly for the sizeof operator (as in the same as above). The parentheses are needed "to translate the operand into the desired type" (as in (int)2.5
), and not as the syntax itselfsizeof
#include <math.h>
sizeof (12 * log(100)); /* same as `sizeof (double);` */
/* without the parenthesis, this would be */
/* (sizeof 12) * log(100); */
source to share