How safe is ++ on a pointer

This is a discussion on How safe is ++ on a pointer within the C Programming forums, part of the General Programming Boards category; Howdy, It's been so long since I even wrote anything in C/C++. But I've been thinking about pointers lately and ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    How safe is ++ on a pointer

    Howdy,

    It's been so long since I even wrote anything in C/C++. But I've been thinking about pointers lately and trying to remember whats safe about them and whats not.

    Lets say I do this.

    Code:
    char * name = "Paul Stovell";
    'name' is really just an integer that has the location in memory of the letter 'P'. Somewhere burried deep inside my computers memory are 13 characters in a string:
    Code:
    | P | A | U | L |   | S | T | O | V | E | L | L | \0 |
    Using * in front of the name after it is declared, for example:
    Code:
    *name = "John";
    I am dereferencing the pointer. If I just used 'name' I would be assigning a char array to an integer, which would be incorrect. But with *, I am saying "get me the char array that 'name' points to".

    Is this understanding correct?

    Because the 'name' variable is really just an integer, I can use ++, --, or all the other integer operators (correct?). When I do:
    name++, I am saying "Name now points to the memory location one byte after its current location".

    If this is true, how sure can we be those bytes will always be in a sequential order? And, how sure can we be that the character at position name+13 is always '\0'?

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    In your example, when you dereference the name pointer, you are saying "to look at what the name pointer is pointing to", which in this case would be a type char. You can use ++ and -- on pointers, since it would just cause it to do pointer arithmetic (not integer arithmetic). So if you tried to increment an integer pointer using ++, usually in most modern machines you would increase the address by 4 bytes not 1.

    name is not an integer, but a pointer variable. It simply points to an address in memory. What name is pointing to is a string literal, i.e. points to a read only memory location that has the value "Paul Stovell", you cannot simply dereference it and assign another value to it since it points to a read-only address. If you want to have write access, then either declare name is an array of characters or malloc() a region of memory to use.
    Last edited by 0rion; 04-20-2005 at 04:48 AM.
    The cost of software maintenance increases with the square of the programmer's creativity.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    how sure can we be those bytes will always be in a sequential order?
    If you initialize the pointer using a string you can be 100% sure the bytes will be in sequential order. You're guaranteed that *name will be P, *(name+1) will be 'a', *(name+2) will be 'u' and so on.
    And, how sure can we be that the character at position name+13 is always '\0'?
    name+13 will add 13 to the address that's stored in name. But the byte at name+13 will not be '\0'. It will be at name+12:
    Code:
    name | name+1 | name+2 | name+3 | ... | name+10 | name+11 | name+12 |
      P      a       u      l           ...       l        l         \0
    When you use pointer arithmetic, the size of the data that the pointer points to comes into play. If you have an int pointer, then ptr+3 will actually be ptr plus sizeof(int)*3 for instance. This is very convenient for stepping through any kind of array using a pointer:
    Code:
    int array[] = { 0, 1, 2, 3 };
    int *ptr = array;
    In the code above, if array starts at memory address 10, then ptr will evaluate to 10, ptr+1 will point to address 10+sizeof(int)*1, ptr+2 will point to address 10+sizeof(int)*2, and so on. Even if you have a pointer to an array of structs, every time you add 1 to the pointer it will add the size of the struct to the memory address automatically. This way you can always be sure that ptr+1 will always point to array[1], ptr+2 will always point to array[2], etc.

    This means you can also trick the compiler into letting you use a certain data type in a different way than you'd normally be able to. For instance:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int num = 0x12345678;
      char *ptr = (char *)&num;
    
      printf("num = %X\n\n", num);
    
      printf("byte 1: %X\n", ptr[0]);
      printf("byte 2: %X\n", ptr[1]);
      printf("byte 3: %X\n", ptr[2]);
      printf("byte 4: %X\n", ptr[3]);
    
      return 0;
    }
    Code:
    num = 12345678
    
    byte 1: 78
    byte 2: 56
    byte 3: 34
    byte 4: 12
    By tricking the compiler by using a pointer to char that actually points to an int, it will step through the number sizeof(char) bytes at a time instead of sizeof(int) bytes at a time. You can easily see from the results that I'm on a little endian machine since the MSB of num is stored in the last byte intead of the first one.
    Last edited by itsme86; 04-20-2005 at 05:16 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by 0rion
    You can use ++ and -- on pointers, since it would just cause it to do pointer arithmetic (not integer arithmetic). So if you tried to increment an integer pointer using ++, usually in most modern machines you would increase the address by 4 bytes not 1.
    The example I heard when I was learning pointers was this: Incrementing a pointer is like telling a guy to go to the next house on the block. It doesn't matter how far apart the houses are, whether they're row homes or mansions with a mile between them. The guy can still walk to the next house on the block. Similarly, when incrementing a pointer, the pointer points at the next element in memory, whether it's a char, int, float or struct. It depends only on what the type of the pointer is.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    I said an "integer" pointer and on "most" machines and it was only an example - I coulda explained it betta but oh well
    The cost of software maintenance increases with the square of the programmer's creativity.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I'm not disagreeing with you or anything, Orion. I was just building on the ideas you presented by providing an example I found helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  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