#include <iostream>
int main()
{
int a[3][3] = {{22, 33, 44}, {55, 66, 77}, {88, 99, 100}};
std::cout << a[1] << '\n' << a + 1 << '\n' << *(a + 1);
}
0x0013FF68
0x0013FF68
0x0013FF68
Why does a+1 == *(a+1)?
a + 1 is the address of the second element in a and could also be written as &a[1] (which is equivalent to &*(a + 1) by definition).
*(a + 1) is an lvalue referring to the second array. It's equivalent to a[1] by definition.
Just like with any other array to pointer decay, this lvalue decays to a pointer to the first element of the array it refers to, i.e. it decays to &a[1][0]. But that is equivalent to the address of that array object itself. So the value is the same as that of &a[1] ... which is precisely how we defined the value of the expression a + 1 above.
Note that the array is decayed to a pointer because the best match for the second insertion is operator<<(void const*). Consider
int (*p1)[3] = a + 1;
int (&p2)[3] = *(a + 1); // We could also have written *p1
int* p3 = p2; // The array-to-pointer decay
assert( static_cast<void*>(p1) == static_cast<void*>(p3) );
Related
So I read online that if you have an array like
int arr[3] = {1,2,3}
I read that if you take (arr+n) any number n it will just add sizeof(n) to the address so if n is an integer(takes up 4 bytes) it will just add 4 right? But then I also experimented on my own and read some more stuff and found that
arr[i] = *(arr+i) for any i, which means it's not just adding the sizeof(i) so how exactly is this working?
Because obviously arr[0] == *(arr+0) and arr[1] == *(arr+1) so it's not just adding sizeof(the number) what is it doing?
I read that if you take (arr+n) any number n it will just add sizeof(n) to the address
This is wrong. When n is an int (or an integer literal of type int) then sizeof(n) is the same as sizeof(int) and that is a compile time constant. What actually happens is that first arr decays to a pointer to the first element of the array. Then sizeof(int) * n is added to the pointers value (because elements type is int):
1 2 3
^ ^
| |
arr arr+2
This is because each element in the array occupies sizeof(int) bytes and to get to memory address of the next element you have to add sizeof(int).
[...] and read some more stuff and found that arr[i] = *(arr+i)
This is correct. For c-ararys arr[i] is just shorthand way of writing *(arr+i).
When you write some_pointer + x then how much the pointer value is incremented depends on the type of the pointer. Consider this example:
#include <iostream>
int main(void) {
int * x = 0;
double * y = 0;
std::cout << x + 2 << "\n";
std::cout << y + 2 << "\n";
}
Possible output is
0x8
0x10
because x is incremented by 2* sizeof(int) while y is incremented by 2 * sizeof(double). Thats also the reason why you get different results here:
#include <iostream>
int main(void) {
int x[] = {1,2,3};
std::cout << &x + 1 <<"\n";
std::cout << &x[0] + 1;
}
However, note that you get different output with int* x = new int[3]{1,2,3}; because then x is just a int* that points to an array, it is not an array. This distinction between arrays and pointers to arrays causes much confusion. It is important to understand that arrays are not pointers, but they often do decay to pointers to their first element.
int arr[] = {0, 1, 15, 22, 100, 101, 232, 2323};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 232;
auto a = lower_bound(arr, arr + n, x) - arr;
if (a < n && arr[a] == x) {
// x found at index a
cout << x << " is found " << "at index: " << a << endl;
}
lower_bound returns an address of the element but if we subtract - arr from it, it dereferences the pointer. How come that happens?
arr is an array, and when an array is used in an expression, it can decay into a pointer. Since lower_bound returns a pointer (that's the iterator type for a raw array), and arr decays into a pointer, you are subtracting the address of the found object from the address of the start of the array which gives you the number of elements between them. There is no dereferencing happening at all.
#include <stdio.h>
int main()
{
int arr[3][4] = {
{ 10, 11, 12, 13 },
{ 20, 21, 22, 23 },
{ 30, 31, 32, 33 }
};
printf("%u, %u", arr, *arr) // Output - 1875181168, 1875181168
}
Assuming any 2D Array (arr), is not a double pointer, then why does arr & *arr refer to the same address ?
If we "draw" the array arr on paper it will look like this
+-----------+-----------+-----------+-----------+-----------+-----+
| arr[0][0] | arr[0][1] | arr[0][2] | arr[0][3] | arr[1][0] | ... |
+-----------+-----------+-----------+-----------+-----------+-----+
From this it should be easy to see that pointers to the array, the first element of the array, and the first sub-element of the first element will all be the same.
That is, &arr == &arr[0] and &arr[0] == &arr[0][0] (which of course means that &arr == &arr[0][0]).
The differences here is the types:
&arr is of the type int (*)[3][4]
&arr[0] is of the type int (*)[4]
And &arr[0][0] is of the type int *
Now as for arr and *arr, you have to remember that arrays naturally decays to pointers to their first elements. This means arr will be equal to &arr[0]. And dereferencing a pointer will be the "value" being pointed to, so *arr is equal to *(&arr[0]) which is then equal to arr[0] which in turn will decay to &arr[0][0].
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;
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.
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.
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.
This is probably a stupid question, but I don't understand why this works:
int** test = new int*[7];
int x = 7;
*(test+1) = &x;
cout << (**(test+1));
test is a pointer to a pointer right? The second pointer points to the array, right?
In my understand I would need to dereference the "test" pointer first to get to the pointer that has the array.
(*test) // Now I have int*
*((*test) + 1) // to access the first element.
Where is my faulty thinking?
int** test = new int*[7];
+------++------++------++------++------++------++------+
| int* || int* || int* || int* || int* || int* || int* |
+------++------++------++------++------++------++------+
is the equivalent of an array with int pointers:
int* test[0]
int* test[1]
...
int* test[6]
this
int x = 7;
*(test+1) = &x;
+------++------++------++------++------++------++------+
| int* || &x || int* || int* || int* || int* || int* |
+------++------++------++------++------++------++------+
is the same as
int x = 7;
test[1] = &x
so now one of the pointers in your original array is pointing the memory location of x
cout << (**(test+1));
is the same as
cout << *test[1]
which is the value of x (==7) and which both test[1] and &x point to.
Is your misunderstanding that you think you have created a pointer to an array of 7 int? You haven't. You actually have created an array of 7 pointers to int. So there is no "second pointer" here that would point to an array. There is just one pointer that points to the first of the 7 pointers (test).
And with *test you get that first pointer which you haven't initialized yet, though. If you would add 1 to that, you would add 1 to some random address. But if you add 1 to test you get a pointer that points to the second pointer of the array. And dererencing that you get that second pointer, which you did initialize.
What you describe would be achieved by a different syntax
typedef int array[7];
array* test = new int[1][7];
// Note: "test" is a pointer to an array of int.
// There are already 7 integers! You cannot make it
// point to an int somehow.
*(*test + 1) = 7;
int *p1 = *test
int i1 = *(p1 + 1); // i1 is 7, second element of the int[7]
delete[] test;
Without using the typedef, this looks like the following
int(*test)[7] = new int[1][7];
That is, you have created a one-element array, where the element-type of that is a 7-element array of int. new gives you a pointer back to that array. Note that the parenthesis is important: The * has less precedence than the [7], so otherwise this would be taken as an array of 7 pointer to integers.
Suppose that
test[0] = 0x12345678; // some pointer value
test[1] = 0x23456789; // some pointer value
*test = 0x12345678;
*test + 1 is now 0x12345678 + 1 = 0x12345679;
* or dereference operator has higher precedence than binary +). So the expression is evaluated in that order.
However what you wanted for is to get to test[0] = 0x23456789;
So the correct expression to get to test[1] = (*(test + 1))
In general arr[i] is *(arr + i)
EDIT 2:
given
int buf[10] = {0, 1, 2};
int *p = buf;
buf[0] == p[0] == *(p + 0) equal to 0.
Note that it is perfectly fine to use array access syntax with the lvalue expression p even if it is not an array type. In fact the expression buf[0] is internally translated by the compiler to *(buf + 0) as well.
The expression *(test + 1) is equivalent to test[1], so your code could be rewritten thus:
int** test = new int*[7];
int x = 7;
test[1] = &x;
cout << *test[1];
Since test[1] obviously points to x, *test[1] is 7.
Just to be clear, the expression **(test + 1) is simply equivalent to *(*(test + 1)), which is in turn equivalent to *test[1].