Thread: pointer to the end of an array

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    pointer to the end of an array

    Code:
    int a[10];
    int *p = a;
    p is the same as &a[0].

    how to write a pointer that points to a[9] (i.e. the last item of the array)?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Are you familiar with the address-of (&) operator?

    EDIT: I didn't read your post thoroughly enough.

    You literally answered your own question though. If a is the same as &a[0], then the pointer to the last array element would be &a[9]. Do you follow?
    Last edited by Elkvis; 01-21-2016 at 07:35 AM.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    I'm a little bit new to addresses and pointers. I'm trying to remember what I know but I can't really figure out why you mentioned the &. Well, if we know the type of the elements of array and the size, we can do this

    sizeof(a)/sizeof(a[0]) // to determine the numbers of array

    then assuming that the code I wrote in the first post is written, we can do this

    p[sizeof(a)/sizeof(a[0]) - 1] // as the index starts at 0

    but i don't think this is a good idea. I can't really think of anything else.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by lmanukyan View Post
    I'm trying to remember what I know but I can't really figure out why you mentioned the &.
    "&" is the "address-of" operator. It is used to get the address of a variable.

    An array name by itself acts as a pointer to its first element.

    Therefore, the following two are equivalent:

    Code:
    // int a[10];
    
    int *p = &a[0];
    
    int *p = a;
    So if you want the pointer to point at the 9th element of the array, you can do as Elkvis suggested:

    Code:
    int *p = &a[9];
    Quote Originally Posted by lmanukyan View Post
    then assuming that the code I wrote in the first post is written, we can do this

    p[sizeof(a)/sizeof(a[0]) - 1] // as the index starts at 0

    but i don't think this is a good idea. I can't really think of anything else.
    This is a sound approach, assuming "p" is already point to "a".

    Just be aware that the "sizeof()" operator can only be used on an array with this approach, not on a pointer.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Well, I wrote this:

    Code:
    int main() {
        int a[10];
        int *pt1 = a; // a pointer to the beginning of the array, i.e. a[0]
        int *pt2 = &a[9]; // a pointer to the end of the array, i.e. a[9]
        printf("%d", pt2-pt1);
        return 0;
    }
    I read that this is supposed to print the length of an array, i.e. 10 but it prints 9. What's the reason?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lmanukyan
    I read that this is supposed to print the length of an array, i.e. 10 but it prints 9. What's the reason?
    The reason is: 9 - 0 == 9. Basically, pt1 points to the element at index 0. pt2 points to the element at index 9. Therefore, pt2 - pt1 == 9.

    You probably read about a one-past-the-end pointer instead:
    Code:
    int a[10];
    int *pt1 = a; // a pointer to the beginning of the array, i.e. a[0]
    int *pt2 = a + 10; // a pointer to one past the end of the array
    printf("%d", pt2 - pt1);
    pt2 doesn't quite point to a[10], since a[10] conceptually does not exist, but if it did exist, that is where it would point. You are not permitted to dereference such a pointer, but you can use it in pointer arithmetic and compare it.
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int *pt1 = a;
    If you wrote this as
    int *pt1 = &a[0];

    Then it should be obvious that
    printf("%d", pt2-pt1);
    is just the same as
    printf("%d", 9-0);
    because everything else cancels out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    And how do i.pass this pointer to thr end of the array to the function???

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by lmanukyan View Post
    And how do i.pass this pointer to thr end of the array to the function???
    It's exactly the same as any other pointer.

  10. #10
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Like thiS?

    Code:
    void someFunction(int *ptArr, int *(ptArr + SIZE-1)); // assuming SIZE has been defined

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No. You are mixing up parameter and argument. The parameter of the one-past-the-end pointer should be the same type as the pointer to the start of the array. When you pass the pointer in, then you start doing all of the weird arithmetic.
    Code:
    void someFunction(int *ptArrBegin, int *ptArrEnd)
    {
       // implement here
    }
    
    // the function's use:
    int arr[SIZE];
    someFunction(arr, arr+SIZE);
    Note that SIZE is one-past-the-end. SIZE-1 is the highest index. Only one of these will be the right thing to use depending on the rest of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cast a struct pointer to an array pointer
    By bremenpl in forum C Programming
    Replies: 1
    Last Post: 12-03-2015, 07:06 AM
  2. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  3. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM