Switch case expression

前端 未结 3 1773
栀梦
栀梦 2021-01-23 08:46

Consider an expression *(1+\"AB\" \"CD\"+1)

What is the solution for this expression ? The above expression is a switch expression in C.

*(2         


        
3条回答
  •  没有蜡笔的小新
    2021-01-23 09:15

    Casabance.

    If you have

    char *cp;
    int i;
    

    then cp[i] == *(cp+i) == *(i+cp) == i[cp].

    C11 6.5.2.1:

    A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).

    And C11 6.5.6:

    When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression.

    A string literal is just a char pointer as far as the compiler is concerned.

    Two string literals next to each other auto concatenate into a single string literal:

    C11 6.4.5:

    In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence.

    So... *(1+"AB" "CD"+1) == *(1+"ABCD"+1) == *("ABCD"+1+1)==*("BCD"+1) == "BCD"[1] == 'C'.

提交回复
热议问题