I have some doubts about string literals.
Firstly, I know that C-strings must be compared using the strcmp()
,strncmp()
or some other function and if one uses ==
, then they compare the pointers as the array name "decays" into a pointer to its first element.
Is there a chance that two same strings can have the same address,i.e, Can these conditions be true?
if("string"=="string")
/*OR*/
char* str="string";
if(str=="string")
/*OR*/
char* str="string";
char* str2="string";
if(str==str2)
Secondly, Can two unequal strings contain the same address?
Lastly, Do these strings stay in read only memory segment until the program ends?