CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

How far does the pointer move?

It would make little sense to be able to ''point anywhere'' into memory, and so C automatically 'adjusts' pointers' movement (forwards and backwards) by values that are multiples of the size of the base types to which the pointer points(!).

In our example:


for(int i=0 ; i<N ; ++i) {
    *p = 0;  // set what p points to to zero   
    ++p;     // advance/move pointer p "right" to point to the next integer   
}

p will initially point to the location of the variable:

  • totals[0], then to
  • totals[1], then to
  • totals[2] ...

Similarly, we can say that p has the values:

  • &totals[0], then
  • &totals[1], then
  • &totals[2] ...

 


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