changing pointers' destination relative to current position

This is a discussion on changing pointers' destination relative to current position within the C++ Programming forums, part of the General Programming Boards category; When you change where a pointer points to in the following manner: Code: char *ptr; ptr++; ptr+=5; ptr--; ptr-=5; Is ...

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    changing pointers' destination relative to current position

    When you change where a pointer points to in the following manner:

    Code:
    char *ptr;
    
    ptr++;
    
    ptr+=5;
    
    ptr--;
    
    ptr-=5;
    Is it relative to the base type? That means to say, in this case it would point one byte ahead, then six, then five, then at it's original spot. If it it was an [32-bit] int pointer, it would point four, twenty-four, twenty, then zero bytes ahead of the original value. Is this correct? I would think so. Or incrementing and decrementing pointer addresses always on the granularity of one byte? Thank you.
    hasafraggin shizigishin oppashigger...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It is relative to the type, just made a test on it.

    Ptr++ increases 4 steps on a 32bitInt, 2 steps on an unsigned short and 16 steps on a structure containing 3 ints and 2 u-shorts .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is this correct?
    Yes, consider arrays. The data type of each element in the array determines how big each element is. If the array is of char, each element is the size of a char, if it is int then each element is the size of an int. Arrays are degraded into pointers like so:
    a[i] == *( a + i )

    If a is of char then *( a + i ) will move the pointer to i char past a, if a is of in then the same action is performed except the pointer will move i int past a. The same thing goes for pointers that were dynamically allocated.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    Hey much thanks there friend. I probably should have tested it myself. And your new information is great in that we can use user defined structures on it too, I wonder if that's how classes would behave... [In DJGPP tho, just for the sake of mentioning, you'd have to use the PACKED attribute so that it's, well PACKED and not aligned on 32-bit bounds per piece of data in the structure] Also I would figure that the data in a class array would be contigious since I've heard [and would figure so] that the classes member function code is only created once, and that only the data is replicated at runtime, and if it's a pointer I'd hope it was adjacent and orderly, other than the PACKED issue.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

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