0
int main()
{
    int arr[3] = { 1,2,3 };
    
    printf("%p\n", arr);
    printf("%p\n", arr + 1);
    printf("%p\n\n", arr + 2);
    
    printf("%p\n", &arr[0]);
    printf("%p\n", &arr[1]);
    printf("%p\n", &arr[2]);
    return 0;
}

output: 

000000A3344FF7E8
000000A3344FF7EC
000000A3344FF7F0

000000A3344FF7E8
000000A3344FF7EC
000000A3344FF7F0

I don't understand the logic behind the operation arr + 1. Since arr is 000000A3344FF7E8, I thought it was going to add 1 resulting in 000000A3344FF7E9. But instead in reality it adds 1*sizeof(element) == 4 resulting in 000000A3344FF7EC.

So my question is, how does the compiler understand pointer arithmetic arr + 1 as not just number addition? And could you elaborate specifically on what arr + 1 is doing.

Hanrabong
  • 35
  • 4

1 Answers1

-1

For anyone who reads this who doesn't know how pointers work:

A pointer is a variable which contains a memory address as value.

char c;
char * c_pointer; // adding "*" in front of the variable name when declaring a variable tells the compiler to expect a memory address instead of a value

c = 'a';
// "&" tells the compiler to get the memory address of the variable
c_pointer = & c; // here we are storing the memory address of "c" inside "c_pointer"
// "*" tells the compiler to go to the memory address stored in the variable and get its value 
printf(* c_pointer); // so in this case we are printing the value of the memory address stored in c_pointer```
jmelger
  • 87
  • 5
  • Arrays are not pointers. They are often implicitly converted to pointers to their first elements, but not always. – HolyBlackCat Mar 08 '22 at 20:58
  • When you declare an array the array variable is a pointer to the first variable in the array. So an array simply is a variable containing a memory address to the first variable in the list. When you declare the array in this case `int arr[3]` you are telling the compiler 3 things: 1. The variable will be an array of size 3 so it will contain a memory address and thus be a pointer. 2. The array will be storing integers 3. the array will be named "arr" The compiler knows the values its supposed to store are (4 byte) integers and if you add 1 to the address it will segfault (i think) – jmelger Mar 08 '22 at 21:03
  • Anyways, there's no real use in adding anything else than a variables own size in bytes to its memory address so the compiler just adds the sizeof([yourvariable]) – jmelger Mar 08 '22 at 21:07
  • This isn't true. Check out [Is an array name a pointer?](https://stackoverflow.com/q/1641957/2752075). – HolyBlackCat Mar 08 '22 at 21:11
  • @HolyBlackCat (actually interested) why does printing an array (like in the question) print a memory address if an array doesn't store a pointer to the first element? – jmelger Mar 08 '22 at 21:16
  • Because, again, arrays are implicitly converted to pointers to their first elements in many cases, and this is one of them. The conversion doesn't happen when you apply `&` or `sizeof` to an array, or (in C++) when you apply `decltype`, or bind it to a reference to an array, etc. – HolyBlackCat Mar 08 '22 at 21:19
  • @HolyBlackCat Oh, didn't know that, always something to learn :) To clarify for me and anyone reading this, the rest of my explanation in this case is still true right? When you `arr + 1` it converts the array to a pointer and so forth... – jmelger Mar 08 '22 at 21:23
  • Yep, it happens in this case too. – HolyBlackCat Mar 08 '22 at 21:26