Sizes of arrays declared with pointers

后端 未结 5 1712
走了就别回头了
走了就别回头了 2021-01-23 14:24
char c[] = \"Hello\";
char *p = \"Hello\";

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

My question is:

Why

相关标签:
5条回答
  • 2021-01-23 14:45

    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.

    0 讨论(0)
  • 2021-01-23 14:52

    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)
    
    0 讨论(0)
  • 2021-01-23 14:52

    "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.

    0 讨论(0)
  • 2021-01-23 14:54

    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.

    0 讨论(0)
  • 2021-01-23 14:55

    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>.

    0 讨论(0)
提交回复
热议问题