Home > Software engineering >  The representation of the array
The representation of the array

Time:05-31

If it is defined as follows:

char x[] = "abcdefg";
char y[] = {'a','b','c','d','e','f','g'};

then why array x is not equivalent to array y?

CodePudding user response:

When you create a manual array, you choose put or not the end character "\0", BUT when the C compiler read the second way to create the array, add automatically the end character, so it's different because have different length.

You can also test with the following example,

#include <stdio.h>

int main(void) {
   printf("%zu\n", sizeof(x));
   printf("%zu\n", sizeof(y));
}
  • Related