2021年1月19日星期二

C++ equal operator in array

const char ca[]="test";  const char cb[]="test";    const char *pca="test"; //(*)  const char *pcb="test";    cout<<(ca==cb)<<endl; //0  cout<<(pca==pcb)<<endl; //1  

I'm quite confused about this result.

ca cb are different objects, when dealing with ca==cb, it compares the address of the first elements, so &ca[0]!=&cb[0].

But I don't understand the behaviour of pca pcb.

Does (*) equivalent to

const char temp[]="test";  const char *pca = temp;  

?

Is "test" an object? and both pca and pcb points to the first element of this object?

https://stackoverflow.com/questions/65802920/c-equal-operator-in-array January 20, 2021 at 12:02PM

没有评论:

发表评论