Layout in initializing an array in C char

Now I am facing a problem as shown below:

int foo(void){
    char a[10] = "foo";
    char b[10] = {"foo"};
    ...
}

I used gdb to check the contents of both "a" and "b", and they looked exactly the same.

Is there a difference between a and b?

+4
source share
2 answers

They are the same:

C11 ยง6.7.9 Initialization

A character type array can be initialized with a character string literal or a UTF-8 string literal , optionally enclosed in braces. Sequential bytes of a string literal (including a terminating null character, if any, or if an array of unknown size) initialize the elements of the array.

+5

, . :

:

(strcmp(a,b)==0) ? cout << "Same" : cout << "Diff";

:

(a==b) ? cout << "Same" : cout << "Diff";
-1

Source: https://habr.com/ru/post/1529919/


All Articles