Sizes of arrays declared with pointers

纵然是瞬间 提交于 2019-12-20 04:53:56

问题


char c[] = "Hello";
char *p = "Hello";

printf("%i", sizeof(c)); \\Prints 6
printf("%i", sizeof(p)); \\Prints 4

My question is:

Why do these print different results? Doesn't c[] also declare a pointer that points to the first character of the array (and therefore should have size 4, since it's a pointer)?


回答1:


It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char * and char []) are not the same thing.

  • An array char a[SIZE] says that the value at the location of a is an array of length SIZE
  • A pointer char *a; says that the value at the location of a is a pointer to a char. This can be combined with pointer arithmetic to behave like an array (eg, a[10] is 10 entries past wherever a points)

In memory, it looks like this (example taken from the FAQ):

 char a[] = "hello";  // array

   +---+---+---+---+---+---+
a: | h | e | l | l | o |\0 |
   +---+---+---+---+---+---+

 char *p = "world"; // pointer

   +-----+     +---+---+---+---+---+---+
p: |  *======> | w | o | r | l | d |\0 |
   +-----+     +---+---+---+---+---+---+

It's easy to be confused about the difference between pointers and arrays, because in many cases, an array reference "decays" to a pointer to it's first element. This means that in many cases (such as when passed to a function call) arrays become pointers. If you'd like to know more, this section of the C FAQ describes the differences in detail.

One major practical difference is that the compiler knows how long an array is. Using the examples above:

char a[] = "hello";  
char *p =  "world";  

sizeof(a); // 6 - one byte for each character in the string,
           // one for the '\0' terminator
sizeof(p); // whatever the size of the pointer is
           // probably 4 or 8 on most machines (depending on whether it's a 
           // 32 or 64 bit machine)



回答2:


The two operands to sizeof have different types. One is an array of char, the other a pointer to char.

The C standard says that when the sizeof operator is applied to an array, the result is the total number of bytes in the array. c is an array of six char including the NUL terminator, and the size of char is defined to be 1, so sizeof (c) is 6.

The size of a pointer, however, is implementation-dependent. p is a pointer to char. On your system, the size of pointer to char happens to be 4 bytes. So that's what you see with sizeof (p).

If you try sizeof(*p) and sizeof(*c), however, they will both evaluate to 1, because the dereferenced pointer and the first element of the array are both of type char.




回答3:


They don't print the same because arrays and pointers are just not the same. There's no reason why they should be the same. An array is implicitly converted to a pointer in many circumstances, but this doesn't make them identical.




回答4:


"sizeof" is the directive of the compilers. The result indicates the size in memory that the argument occupies. Compiler is reseponsible for determining the result according to the argument's type. Regarding the "array", returns the array size. moreover, the pointer returns "4" generally speaking for 32-bit machine.




回答5:


pointer and array are different.

char c[] = "Hello"; //declare c as an array. sizeof(c) calculate the size of array 6.
char *p = "Hello"; //declare p as a pointer.sizeof(p) calculate the size of pointer,in most machine the size of pointer is 4.

you can also refer to <c traps and pitfall>.



来源:https://stackoverflow.com/questions/18414310/sizes-of-arrays-declared-with-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!