jraavis icon

C Pointer Riddle

jraavis | PRO | 06/19/17 07:19:33 PM UTC | 0 ⭐ | 320 👁️ | Never ⏰ | []
C |

820 B

|

None

|

0 👍

/

0 👎

#include <stdio.h>
 
int main() {
    int a=2;
    int *p=&a; /* *p = 1, p = 1004 */
    printf("Line 1: %u %u\n",*p, p);
    *p++; 
    /* 
    p++ = 1004 (incrementing p: operation not completed at)
    *(p++) = increment'ing' p..! it's pointer, allotted with int 4bits again so *p = 1008 
    (==== it incrementing pointer not pointer pointing value. ====)
    *p = 1008
    */
    printf("Line 3: %u %u\n",*p, p);
    ++(*p); 
    /*
    *p = 1008, p = 1008 
    *p p's pointer
    p's incrementing p's pointer value, so p value (1008) added by 1.
    so p = (p + 1); p = 1008+1; p = 1009 and *p some come with some new location 3001
    */
    printf("Line 4: %u %u\n",*p, p);
    --p;
    /*
    (p = p-1) no more old pointer address but direct value.
    p = 1009-4; p = 1005; and some new random p's address *p = 3420
    */
    printf("Line 5: %u %u\n",*p, p);
    return 0;
}

Comments