1

In the following I expected 13 to be printed.
I wanted to move arr (which is a pointer to the memory, where int values from array are stored, if i understand everything right) by the size of one array member, which is int.

Instead 45 is printed. So instead making one array-member-wide jump the 5th Array member is retrieved. Why?

int arr[] = {1,13,25,37,45,56};
int val = *( arr + 4 );         //moving the pointer by the sizeof(int)=4
std::cout << "Array Val: " << val << std::endl;
1
  • This pertains to behavior of standard pointer arithmetic for arrays. More can be found here Commented Sep 13, 2016 at 2:16

3 Answers 3

10

Your assumption is wrong. It moves the pointer 4 elements ahead, not 4 bytes ahead.

*(arr + 4) is like saying in that logic *(arr + 4 * sizeof (arr [0])).

The statement *(arr + 4) is equivalent to arr [4]. It does make for some neat syntax, though, as *(4 + arr) is equally valid, meaning so is 4 [arr].

Your behaviour could be achieved through the following example:

#include <iostream>

int main()
{
    int a[3] = {65,66,67};
    char *b = reinterpret_cast<char *>(a);
    std::cout << *(b + sizeof (int)); //prints 'B'
}

I wouldn't recommend using reinterpret_cast for this purpose though.

Sign up to request clarification or add additional context in comments.

9 Comments

so this is a syntax thing? (arr+x) accesses the array? How can I change the memory address then?
The arr + x part is part of pointer arithmetic rules. arr + 1 advances to the next element of the type that arr points to.
aha - this I wanted to understand. And what happens, if the type of the pointer is void* ?
Why should that print B? There's no justification for it, and, on a big endian machine, it won't.
@jpalecek, that precise example prints B for me and doesn't give any warnings, but you're correct in saying that. I did recommend not to do it.
|
4

arr + 4 will give you 4 items on from the start address of the array, not 4 bytes. That's why you get 45 which is the zeroth item plus 4.

Comments

0

It is performing pointer arithmetic. See this: http://www.eskimo.com/~scs/cclass/notes/sx10b.html

arr is your array and this decomposes to the first element arr[0] which will be 1, then you + 4 to it which moves the pointer by 4 elements arr[4] so it is now pointing to 45.

2 Comments

So then it should be 5? Because 4 plus the first_array_element(which is 1) should be 5?
@Skip No, your array is composed of ints, when you increment the pointer you are moving the pointer to the next element you are not adding 4 to the value of the first element int val = (*arr) + 4 ; this would produce 5 as you dereference the first element which is 1 and then add 4 what you are doing is incrementing the pointer by 4 positions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.