Menu

Pointing at a Place

9v1cl0
2007-11-05
2012-09-26
  • 9v1cl0

    9v1cl0 - 2007-11-05

    I have been working with pointers, and I am curious is it possible to add one (in hex) to a pointer so that if the location of the pointer is displayed I would get 0x445021 instead of 0x445020. I know that by saying *pa++ and the original address is 0x445020 i would get 0x445028. Or is it possible to force a variable to be at a certain location?
    Thanks

     
    • Anonymous

      Anonymous - 2007-11-05

      Pointer arithmetic works in units determined by teh size of teh object to which it points.

      Your point about *pa++ id not right, you dereferncing the pointer, so it would incremented the value pointerd to. If you meant pa++, the statement is only true if sizeof(pa) = 8 (a long long for example). If pa were a pointer to a char, the address increment would indeed be 1.

      Consider teh following code. Note how the address after ++ is determined by the pointer type.
      int main()
      {
      int array[] = { 0x11223344,0x55667788,3,4,5,6,7,8,9 };
      int pa = array ;
      char
      pb = (char*)array ;

      printf( "%p, %p\n", pa, pb ) ;
      printf( "%X, %X\n\n", *pa, *pb ) ;
      pa++ ;
      pb++ ;
      printf( "%p, %p\n", pa, pb ) ;
      printf( "%X, %X\n", *pa, *pb ) ;
      
      system("PAUSE");
      return 0;
      

      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.