문제

Now I have encounter a problem as below:

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

I used gdb to inspect the content of both 'a' and 'b' and they looked exactly the same.

Is there any difference between the variabel a and b?

도움이 되었습니까?

해결책

They are the same:

C11 §6.7.9 Initialization

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

다른 팁

They contain the same value but are pointing to different addresses. Give a try:

Value:

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

Pointer:

(a==b) ? cout << "Same" : cout << "Diff";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top