CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Dereferencing a pointer, continued

We now know that changing the value that a pointer points to does not change the pointer (good!).

Now we'd like to change the value held in the address that the pointer points to.

Similarly, this will not change the pointer itself.


int total;
int *p     = &total ;
int bigger;

    total  =  8;

    printf("value of variable total is:       %i\n",   total );
    printf("value pointed to by pointer p is: %i\n\n", *p );

    *p  =  *p + 2 ;     // increment, by 2, the value that p points to   

    printf("value of variable total is:       %i\n",   total );
    printf("value pointed to by pointer p is: %i\n\n", *p );

    bigger  =  *p + 2 ; // just fetch the value that p points to   

    printf("value of variable total is:       %i\n", total );
    printf("value of variable bigger is:      %i\n", bigger );
    printf("value pointed to by pointer p is: %i\n", *p );

 


CITS2002 Systems Programming, Lecture 11, p5, 26th August 2024.