Pointer addess...

This is a discussion on Pointer addess... within the C++ Programming forums, part of the General Programming Boards category; If i have a pointer that points to "0012FF53" then i want to increment that pointer to view "0012FF63" for ...

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Pointer addess...

    If i have a pointer that points to "0012FF53" then i want to increment that pointer to view "0012FF63" for example, would have have to declare another variable and get my pointer to point to the new variable? or is there a way to increment a pointer? and would the OS (xp) allow me to view that memory?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    pointer arithmetic allows you to increment a pointer by a certain integer/hex value but unless the pointer lies within a boundary of memory that you have already allocated you could end up trying to access memory that you aren't supposed to. It can do nothing, or it can cause an access violation message.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The pointer type is important. Incrementing an int pointer will have different results from incrementing a char pointer, for example (in most cases).

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int object;
       int *iptr = &object;
       char *cptr = (char*)&object;
       printf("iptr = %p, cptr = %p\n", (void*)iptr, (void*)cptr);
       ++iptr;
       ++cptr;
       printf("iptr = %p, cptr = %p\n", (void*)iptr, (void*)cptr);
       iptr += 10;
       cptr += 10;
       printf("iptr = %p, cptr = %p\n", (void*)iptr, (void*)cptr);
       return 0;
    }
    
    /* my output
    iptr = 0022FF74, cptr = 0022FF74
    iptr = 0022FF78, cptr = 0022FF75
    iptr = 0022FFA0, cptr = 0022FF7F
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21