问题
[Original title referred to '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 is 4? So what arguments can sizeof can actually take?
回答1:
It takes a type.
sizeof(char)
is always one. The variable p
itself is a pointer, and on your platform that has a size of 4. Then you do &p
, or a pointer to a pointer, which also has a size of 4.
On most modern desktop systems, a 32-bit architecture will have 4 byte pointers, while a 64-bit architecture will have 8 byte pointers.
sizeof
itself is a keyword, resolved at compile-time, not a function. In C99, arrays can be variable length, and sizeof will wait until run-time to resolve this size.
回答2:
sizeof
isn't a function, it's a keyword. You could drop the parentheses and it would work just fine. Because it's not a function, it works with any type or object that you give it - it's much more flexible than a function.
回答3:
sizeof
is not a function; it's an operator. It can be used in two ways: as sizeof(typename)
and as sizeof expression
. The parentheses are required when used with a type name. Parentheses are not needed when the operand is an expression, though for clarity's sake many programmers will parenthesize the expression regardless. Note that unlike most programming languages operators, sizeof expression
does not evaluate its argument under normal circumstances, i.e., when its operand is not a C99 variable-length array.
回答4:
Becuase you are working on 32bit machine.
*p is a character == 1 byte.
p is a pointer == 4 bytes.
&p is an address of a pointer == 4 bytes. Same as char**
回答5:
Also note:
sizeof 'a' == 1 // C++
but
sizeof 'a' == 4 /* C */
(To be precise, sizeof 'a' == sizeof(int) in C as posted by pmg).
In C, the 'a' gets promoted to an int before it is evaulated. One of the few incompatibilities between the two languages.
回答6:
sizeof (char) = 1
sizeof (void *) = 4
The first one is 1 because you are passing a char. (p points to a char) 2nd and 3rd calls receive a char* / void* object, so they yield 4.
回答7:
The first answer is saying that a char is 1 byte. (derefing your pointer to a the actual char data)
The second is saying that the pointer value is a 4 byte value, that is the memory location is represented by a 4 byte value.
The third is saying that the address of the pointer is a 4 byte value.
Size of can take all of those arguments, and it is reporting accurate response for all of the values.
Cheers.
回答8:
In addition to the other replies, beware of the possible terminological mixup.
In C world, 'byte' is the same as 'char'. I.e. each 'char' is 1 'byte' by definition. This meaning of 'byte' is not necessarily connected to the machine 'byte' in the underlying hardware. A C 'byte' (i.e. 'char') can consist of several machine 'bytes'. However, in C program you have no access to machine bytes. Everything is measured in C 'bytes' (i.e. in 'chars') and 'sizeof' gives your the size measured in 'chars'.
For these reasons, 'sizeof(char)' is always 1 regardless of how many machine bytes each 'char' actually consists of.
回答9:
The operand of the sizeof
operator is a type.
If you use sizeof with an object or a 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 a type directly to the sizeof operator (as in the "same as" above). The parenthesis are needed "to cast the operand to the right type" (as in (int)2.5
), not as syntax for sizeof
itself
#include <math.h>
sizeof (12 * log(100)); /* same as `sizeof (double);` */
/* without the parenthesis, this would be */
/* (sizeof 12) * log(100); */
回答10:
Size of operator show the size of which data type we used
#include<stdio.h>
int main()
{
int a;
float b;
char c;
double d;
printf("%d",sizeof(a));
printf("%d",sizeof(a));
printf("%d",sizeof(a));
printf("%d",sizeof(a));
}
Answer a=4;
b=4;
c=1;
d=8
If you are using 8 bit compiler.
来源:https://stackoverflow.com/questions/1557362/what-arguments-does-the-sizeof-operator-take-in-c