"Pointer" plus "Number"

This is a discussion on "Pointer" plus "Number" within the C Programming forums, part of the General Programming Boards category; Hi all, let's say we have an array of char (thus array of "bytes"): Code: char pool[1000]; and let's say ...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question "Pointer" plus "Number"

    Hi all,
    let's say we have an array of char (thus array of "bytes"):
    Code:
    char pool[1000];
    and let's say the 1st element of array is stored at address "00000000", the 2nd one at "00000001", the 3rd one at "00000002", ... , and the last one at "00000999":
    Code:
    printf("%d\n", &pool[0]);   // == 00000000
    printf("%d\n", &pool[1]);   // == 00000001
    printf("%d\n", &pool[2]);   // == 00000002
    
                                            .
                                            .
                                            .
    
    printf("%d\n", &pool[999]);   // == 00000999



    And now the problem begins.
    Let's say we have "a pointer to int" which we will give the address of the 1st element in array:
    Code:
    int* pInt = &pool[0];   // It takes "4" first elements in array (sizeof(int) == 4)
    And now if we would like to "add some number to that pointer", let's say:
    Code:
    pInt = pInt + 1;   // "pInt" now points to address "0000003" and no to "00000001"
    But "I want to" do that addition so that pointer will point to the address "00000001"!
    Is there any way to do it?


    Thanks.
    Petike

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You would have to say something like

    Example:
    Code:
    *pInt = *pInt + 1;

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I should have also mentioned you could also do this:

    Example:
    Code:
    *(pInt + 1) = 5;     // which is the same as pInt[1] = 5
    *(pInt + *pInt) = 5; // which is the same as pInt[pInt[0]] = 5

  4. #4
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Hi master5001,
    no I don't want to do anything with values, I would like to add to the pointer "pInt" a number for example 1, so that it would point to the address "1 byte" next to pInt and no "4 bytes" next to pInt.
    Any ideas?
    Petike

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sure.

    Example:
    Code:
    pInt = (char *)pInt + 1;

  6. #6
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Ohh, thaaaaaank you.
    I can't believe I didn't try it before.

    Now it works.

    Thanks master5001
    Petike

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No problem bud.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Perhaps you could have done it like this as well.

    Code:
    char* pInt = &poo; 
    
    pInt += 1;
    printf("%c", *pInt)
    
    or
    
    printf("%c", *(pInt + 1) );
    So that the internal scalar value would be set up to 1 byte, which means when you increment the pointer by 1. It moves one byte forward. Consequently if you declare it as int, it moves 4 bytes from the base memory location. And it is also advised to use the following methods to inclement the pointer.

    Code:
    *(ptr + 1);
    The former would lead you to miss the base memory location, unless you have taken a backup of it.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is all well and good, but it means that pInt would actually be a pointer to a char. Which is not the same as being a pointer to an int.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I removes the explicit type casting.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which is good for moving the pointer, but he wanted the data to be treated as having integer width.

    Let me word this differently. A char * moves in one byte increments but can only access single byte spans of data. An int * moves in sizeof int increments (so 2, 4 or 8 usually) and accesses a span of sizeof int bytes (so 2, 4 or 8 depending on the machine).

    The OP wants a pointer that moves like a char * but reads spans as large as an int *. So that is why your code is misleading, ssharish2005. It doess not read spans sizeof int in width.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by Petike View Post
    Hi all,
    But "I want to" do that addition so that pointer will point to the address "00000001"!
    Is there any way to do it?
    Yes, but it is a very bad idea.
    On some systems you can read from an unaligned address as you want. However being unaligned it takes a least twice as long as internally it does two aligned fetches, and then combines them to give you the desired result.
    On other system's you'll get some kind of crash such as a bus error.

    So my advice is to forget about trying to do that, and just make sure everything is aligned according to the size of the data type instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

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