Thread: Pointer to array behavior question.

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    5

    Pointer to array behavior question.

    When a pointer to an array increments to iterate across the array. Does this action create an array of pointers like:

    ---------
    ptr points to an array &a[0], when ptr increments:
    ---------
    ptr+1 would point to &a[1]
    ---------
    |
    |
    and so on.......

    where ptr+1 memory block is right after ptr, and ptr+2 is right after ptr+1, ......
    Also, my other question, if the memory block, right after ptr is used, by some other variable, does ptr+1 use the next open memory slot, or does the program throw an error, at compile time?
    Thank you guys,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,237
    Quote Originally Posted by n@bu
    When a pointer to an array increments to iterate across the array. Does this action create an array of pointers like:
    No, it is just the value of the pointer changing to point to the next element, which happens to be an array.

    Quote Originally Posted by n@bu
    Also, my other question, if the memory block, right after ptr is used, by some other variable, does ptr+1 use the next open memory slot, or does the program throw an error, at compile time?
    Nothing much: it means ptr+1 points to one-past-the-end of the array, which is fine as long as you make no attempt to dereference it. This is useful for iterating, e.g.,
    Code:
    const int numbers[] = {0, 1, 2, 3};
    const int *ptr = numbers;
    const int *end = numbers + sizeof(numbers) / sizeof(numbers[0]);
    for (; ptr != end; ++ptr)
    {
        printf("%d\n", *ptr);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    5
    Thanks, got it....

    ./ptr2array
    *ptr = 0, &ptr= 0x7fff5521bb08
    *ptr = 1, &ptr= 0x7fff5521bb08
    *ptr = 2, &ptr= 0x7fff5521bb08
    *ptr = 3, &ptr= 0x7fff5521bb08

  4. #4
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by n@bu View Post
    When a pointer to an array increments to iterate across the array. Does this action create an array of pointers like:

    Thank you guys,
    Remember that an array is a sequentially accessed list. Therefore, a pointer to an array, does not create a second array, you are merely moving to the next point in the array list.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by Terrance View Post
    Remember that an array is a sequentially accessed list.
    You can access it any way you want. What you probably meant was that an array is a continuous list of elements, which no gaps in between.
    Devoted my life to programming...

  6. #6
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by GReaper View Post
    You can access it any way you want. What you probably meant was that an array is a continuous list of elements, which no gaps in between.
    I probably should have said that an array is a linear list, but tends to be stored in sequential memory units.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-14-2013, 08:21 PM
  2. pointer/ array question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 05:06 PM
  3. Pointer Array Question
    By d320 in forum C Programming
    Replies: 5
    Last Post: 07-11-2008, 11:51 PM
  4. Normal iterator pointer behavior
    By SevenThunders in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2008, 12:11 PM
  5. A question between Array and Pointer
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2002, 09:13 AM

Tags for this Thread