Pointer operations and operator precedence in C

后端 未结 6 868
野性不改
野性不改 2021-02-04 03:32

Background

Just had a chat with a C guy today and we disagreed on the following:

int intgA[2] = { 1, 2 };
int intgB[2] = { 3, 5 };

int *intAPtr = intg         


        
6条回答
  •  执笔经年
    2021-02-04 04:20

    Example code (In linux):

    #include 
    #include 
    int main() {
        int intgA[2] = { 1, 2 };
        int intgB[2] = { 3, 5 };
        int *intAPtr = intgA; //1
        int *intBPtr = intgB; //3
        *intAPtr++ = *intBPtr++;
        // *intAPtr = *intBPtr;
        // *intAPtr++ = 25; 
        printf("op: %d %d\n", *intAPtr, *intBPtr);
    
       return 0;
    }
    

    output:

    op: 2 5
    

    first its assigning intBPtr to intAPtr then increment take place, Because it is post increment.

提交回复
热议问题