What is the reason that gcc adds char* (e.g. “STRING”) and char (e.g. ‘C’) as pointers?
const char *ccc = "Test1";
const char t = 'T';
const char *res = ccc + t;
printf("%s, %p, %d, %p\n", res, ccc, t, res);
outputs
, 0x8048d97, 84, 0x8048deb
I mean, can you point to the documentation, standard specs, or an article? Can I control or disable this behavior?
UPD: Why I ask and what is unexpected, is that
CString() + 'c'
works as
(char*)CString() + (char)char_var
when compiler cannot find appropriate operator +. I thought maybe to disable automatic concatenation and find all such places (in legacy code). But mostly I just wanted to find exact documentation for the behavior.
In
ccc + t,tis treated as an integer. The net effect is thatrespoints tocccplus 84 bytes, where 84 is the ASCII code of'T'.It is worth pointing out that
ccc + toperates purely on the pointers, and does not touch the actual string. I am saying this in case there’s any expectation that"Test" + 'T'might append the character to the string — it does not.