p is the same as &a[0].Code:int a[10]; int *p = a;
how to write a pointer that points to a[9] (i.e. the last item of the array)?
p is the same as &a[0].Code:int a[10]; int *p = a;
how to write a pointer that points to a[9] (i.e. the last item of the array)?
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?
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.
"&" 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:
So if you want the pointer to point at the 9th element of the array, you can do as Elkvis suggested:Code:// int a[10]; int *p = &a[0]; int *p = a;
This is a sound approach, assuming "p" is already point to "a".Code:int *p = &a[9];
Just be aware that the "sizeof()" operator can only be used on an array with this approach, not on a pointer.
Well, I wrote this:
I read that this is supposed to print the length of an array, i.e. 10 but it prints 9. What's the reason?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; }
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.Originally Posted by lmanukyan
You probably read about a one-past-the-end pointer instead:
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.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);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> 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.
And how do i.pass this pointer to thr end of the array to the function???
Like thiS?
Code:void someFunction(int *ptArr, int *(ptArr + SIZE-1)); // assuming SIZE has been defined
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.
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.Code:void someFunction(int *ptArrBegin, int *ptArrEnd) { // implement here } // the function's use: int arr[SIZE]; someFunction(arr, arr+SIZE);