#include<stdio.h> int main() { switch(*(1+"AB" "CD"+1)) { case 'A':printf("A is here"); break; case 'B':printf("B is here"); break; case 'C':printf("C is here"); break; case 'D':printf("D is here"); break; } }
Conclusion: C is here.
Can someone explain this to me that it confuses me.
First of all, string literals, separated only by a space (and comments), are combined into single lines. This happens before parsing the expression (see, for example, this link to the transition phase for more information). This means that the expression is *(1+"AB" "CD"+1)really parsed as *(1+"ABCD"+1).
*(1+"AB" "CD"+1)
*(1+"ABCD"+1)
, , , , "ABCD", , .
"ABCD"
-, p index i *(p + i) p[i]. , *(1+"ABCD"+1) ( , *("ABCD"+2)) "ABCD"[2]. . 'C'.
p
i
*(p + i)
p[i]
*("ABCD"+2)
"ABCD"[2]
'C'
C , "AB" "CD", . ( , , , PRIx64 <inttypes.h>.) : "ABCD".
"AB" "CD"
PRIx64
<inttypes.h>
- . . ( , , , sizeof.) , "ABCD" A.
sizeof
A
( ), . , 1+"ABCD" B. 1+"ABCD"+1 C.
1+"ABCD"
B
1+"ABCD"+1
C
* , , *(1+"ABCD"+1) C, C.
*
It switch(*(1+"AB" "CD"+1))is rated as switch(*(2+"ABCD")). * (2+"ABCD")indicates a symbol C. therefore the output of your code C is here.
switch(*(1+"AB" "CD"+1))
switch(*(2+"ABCD"))
(2+"ABCD")
C is here
*(any thing) evaluated as a pointer to a string literal.
*(any thing)
Source: https://habr.com/ru/post/1690390/More articles:Why can't gitlab-runner clone my project? (Incorrect host name, failed to connect) - gitHow can I build the surface of a structure that is defined by vectors in python? - pythonReact Native Flatlist - VirtualizedList: you have a large list that is slowly updating - react-nativehow to use matplotlib PATH to draw a polygon - pythonWhy does the C ++ 17 GCC compiler warn about undefined? - c ++Taking the address of a function from within this function - cScala throws an exception when I try to run it on ubuntu - javaПризнать шаблон: какая система порождает параметры строки запроса? - query-stringС++ Использование лямбда для неявного вызова конструктора, ожидающего указателя функции - c++How to populate Pandas dataframe as a function of index and columns - pythonAll Articles