Pointer arithmetic and dereferencing - c++

In the following code, can anyone please explain to my what the line in bold is doing.
struct southParkRec {
int stan[4];
int *kyle[4];
int **kenny;
string cartman;
};
int main()
{
southParkRec cartoon;
cartoon.stan[1] = 4;
cartoon.kyle[0] = cartoon.stan + 1;
cartoon.kenny = &cartoon.kyle[2];
*(cartoon.kenny + 1) = cartoon.stan; //What does this line do?
return 0;
}

Think of it as
cartoon.kenny[1] = cartoon.stan;
They are basically the same thing

If we bring the whole thing to one common style of using the subscript operator [] (possibly with &) instead of a * and + combination, it will look as follows
cartoon.stan[1] = 4;
cartoon.kyle[0] = &cartoon.stan[1];
cartoon.kenny = &cartoon.kyle[2];
cartoon.kenny[1] = &cartoon.stan[0];
After the
cartoon.kenny = &cartoon.kyle[2];
you can think of kenny as an "array" of int * elements embedded into the kyle array with 2 element offset: kenny[0] is equivalent to kyle[2], kenny[1] is equivalent to kyle[3], kenny[2] is equivalent to kyle[4] and so on.
So, when we do
cartoon.kenny[1] = &cartoon.stan[0];
it is equivalent to doing
cartoon.kyle[3] = &cartoon.stan[0];
That's basically what that last line does.
In other words, if we eliminate kenny from the consideration ("kill Kenny"), assuming that the rest of the code (if any) doesn't depend on it, your entire code will be equivalent to
cartoon.stan[1] = 4;
cartoon.kyle[0] = &cartoon.stan[1];
cartoon.kyle[3] = &cartoon.stan[0];
As for what is the point of all this... I have no idea.

In cartoon you have:
- stan, an array of 4 ints.
- kyle, an array of 4 pointers to int.
- kenny, a pointer to a pointer to int, that is, let's say, a pointer to an "array of ints".
cartoon.stan[1] = 4; sets second element of stan array (an int) to 1.
cartoon.kyle[0] = cartoon.stan + 1; sets first element of kyle array (a pointer to int) to point to the second element of stan array (which we have just set to 4).
cartoon.kenny = &cartoon.kyle[2]; sets kenny pointer to point to the third element of kyle array.
*(cartoon.kenny + 1) = cartoon.stan; sets fourth element of kyle array (a pointer to int) to point to the first element of stan array (which hasn't been initialised yet). More in detail:
cartoon.kenny gets kenny pointer's address (third element of kyle array),
cartoon.kenny+1 gets the next int after that address (fourth element of kyle array, which happens to be a pointer to int),
*(cartoon.kenny + 1) dereferences that pointer, so we can set it, and
= cartoon.stan sets it to point to the first element of stan array.

It is incrementing the pointer at *cartoon.kenny. 'kenny' is a pointer to a pointer, so the first dereference returns a pointer, which is incremented, and a value assigned. So, *(kenny + 1) now points to the beginning of the array 'stan'.

It sets the last element of Kyle to point to the first element of Stan.

Related

How can I subtract from elements in an array with pointer notation?

I have a prompt asking me to declare and initialize an array of 4 doubles and to create a pointer to the array. Then I should add 30.0 to the second physical element of the array through the pointer via array notation. After, I should subtract 10.0 from the last array element through the pointer using pointer notation. Then I should reassign the pointer to the last element of the array.
I have tried figuring out how to subtract with pointer notation but no dice. I’m not following what needs to be done.
Here’s my code so far:
int main () {
double arr[4] = {10.0, 15.0, 20.0, 25.0};
double *p = &arr;
p[1] = 30.0; //array notation
//code should go here for the subtracting ten part
//code should go here assigning pointer to last element of array.
//My idea of how this would look:
p = (p + 3); // or 15.0
//or:
p = arr[3];
}
I tried doing something like (p+3) -=10.0 but I have a feeling that’s wrong.
What I think the outcome should be: my arr elements being {10.0, 30.0, 20.0, 15.0} and p pointing to last element of array.
So array notation is really syntactic sugar over pointer arithmetic. In your code:
p[1] = 30.0;
Can also be written as
*(p + 1) = 30.0;
Similarly, if you want to "subtract 10.0 from the last array element through the pointer using pointer notation"
You would do this:
double *p = arr
*(p + 3) -= 10.0
Explanation:
When you are declaring an array you are declaring a series of values stored in contiguous memory (that means memory blocks next to each other). The reason you are able to access different elements in that array is that you know
Where the array starts
How far along you want to move
So really, array notation arr[2] (where arr is an array of doubles) means "Go to the memory address of array arr, move along 2 * sizeof(double), then give us the value stored there. Funnilly enough, that's exactly what *(p + 2) means, only it's broken down - the bit inside the brackets means "move along 2", and the star (the "dereference operator") means give us the value.

Arrays and pointers arithmetic confusion

#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
Fairly new coder here. This is an example taken from the pointers page on cplusplus.com. They talk about dereferencing, the address of operator, and then they talk about the relationship between arrays and pointers. I noticed if I simply printed out a declared array it spits out the address of the array. I'm really trying to understand exactly what some of these lines of code are doing and am having a hard time.
Everything makes sense up until *p=10. I read this as, "the value being pointed to by pointer p is equal to 10" but an array isn't just one value. In this case it's 5... So, whats the deal? Will the compiler just assume we are talking about the first element of the array?
Even more confusing for me is p++;. p right now is the address of the array. How do you do p=p+1 when p is an address?
I understand p= &numbers[2];. As well as the following *p = 30;.
p = numbers + 3; confuses me. Why are we able to take an address and just add 3 to it? And, what does that even mean?
Everything within the for loop makes sense. It's just printing out the array correct?
Thanks for your help in advance!
As Pointed out this due to pointer arithmetic.
So both array numbers and pointer p are int, so after assigning numbers's base address to pointer p and the *p=10 does numbers[0]=10
Now when you do p++ the address of p is incremented but not to the next numeric value as in normal arithmetic operation of ++ but to the address of the next index of numbers.
Let's assume a int is 4byte of memory and numbers's initial address is 1000.
So p++ makes address it to be incremented to 1004 which is numbers[1]
Basically it points to the next int variable position in memory.
So when *p = 20 is encountered the value 20 will be assigned to the next adjacent memory i.e. 1004 or numbers[1]=20.
After that the p directly assigned address of 3rd index numbers[2] i.e 1008 (next int var in memory). Now p is pointing to the 3rd index of numbers so *p=30 is again numbers[2]=30
p = numbers + 3; *p = 40; does similar thing that is assigns base address of numbers incremented by 3 indexes (1000 + 3*sizeof(int) =1012 ==> 1000 + 3*4 =1012) to p and assigns 40 to that. so it becomes equivalent to numbers[3]=40.
Then p = numbers; *(p+4) = 50; is assigning 1000 to p and the adding 4 to the address of p by using the () and assigns 50 to the value by using *() which is value of the address(1000+ 4*4 = 1016) inside the braces.
Using a pointer like this in C++: p = numbers makes p point to the first element of the array. So when you do p++ you are making p point to the second element.
Now, doing *p = 10 assigns 10 to whatever p is pointing to, as in your case p is pointing to the first element of the array, then *p = 10 is the same as numbers[0] = 10.
In case you see this later: *(p + i) = 20, this is pointer arithmetic and is the same as p[i] = 20. That's why p[0] = 10 is equivalent to *(p + 0) = 10 and equivalent to *p = 10.
And about your question: How do you do p=p+1 when p is an address?: as arrays in C++ are stored sequentially in memory, if the element p is pointing at memory address 6400010 then p++ points at 6400014 assuming each element pointed by p occupies 4 bytes. If you wonder why p++ is 6400014 instead of 6400011, the reason is a little magic C++ does: when increasing a pointer, it doesn't increase by 1 byte but by 1 element so if you are pointing to integers, each one occupying 4 bytes, then p will be increased by 4 instead of 1.
Let's break it down:
int numbers[5];
int * p;
Assigning the array variable to a pointer means that the pointer will be pointing to the first element:
p = numbers; // p points to the 1st element
*p = 10; // 1st element will be 10
p++; // go to next location i.e. 2nd element
*p = 20; // 2nd element will be 20
You can do pointer arithmetic and it works according to the type. Incrementing an int pointer on a 32-bit machine will do a jump of 4 bytes in memory. On a 64-bit machine, that will be 8 bytes. Keep in mind that you cannot do this with a void pointer.
p = &numbers[2]; // assign the address of 3rd element
*p = 30; // 3rd element will be 30
p = numbers + 3; // numbers + 3 means the address of 4th element
*p = 40; // 4th element will be 40
p = numbers; // p points to the 1st element
*(p + 4) = 50; // p + 4 means the address of 5th element
// dereference 5th location and assign 50
// 5th element will be 50
There are a lot of pages on the web on how pointers work. But there are a couple things to quickly point out.
int* p;
The above line mean p is a pointer to an int. Assigning something to it doesn't change what it is. It is a pointer to an int, not an array of int.
int number[5];
p = number;
This assignment takes the address where the array is stored, and assigns it to p. p now points to the beginning of that array.
p++;
p now points to the 2nd int in the array. Pointers are incremented by the size of the object they point to. So the actual address may increase by 4 or 8, but it goes to the next address of an int.
*p = 30;
This is an assignment, not a comparison. It doesn't say that what p points to is 30, it copies 30 into the address pointed to by p. Saying "equals" is generally confusing in programming terms, so use either "assign" or "compare" to be clear.
1> p=numbers;
pointer p now points to the start of the array, i.e first element.
2> *p=10;
first element now becomes 10, as p is pointing to the first element.
3> p++;
this moves the pointer to the next element of the array, i.e second element
So, again *p=20; will make second element=20
4> p = &numbers[2];
this statement means that p now points to 3rd element of the array.
So, *p = 30; will make 3rd element=30
5> p = numbers + 3;
Wen we do p=numbers+1; it points to second element, so p=numbers+3; will now point to 4th element of array. *p = 40; make the fourth element=40
6> p = numbers;
Again p points to first element of array
*(p+4) = 50; will now make 5th element=50

Why do you have to specify an index number when assigning a value to the first element in an array?

Why do you have to specify an index number when assigning a value to the first element in an array?
Let's say we had this array:
int childer[] = {10, 30, 50};
then both childer and childer[0] would be accessing the first element of the array, so why can't you dochilder = 15;?
Arrays are similar to pointers. For example an array decays to a pointer when passed to a function, or even when the operators [] and the unary * are applied to them (Thanks StoryTeller). So
childer[0]
is equivalent to
*childer
and you can do
*childer = 15;
to set the first element to 15.
childer
on the other hand is the same as
&childer[0]
childer[0] and childer have two different meanings. childer[0] is the first element of the array while childer is a pointer to the first element. In order to get the first element using the pointer, you have to use *childer.
so,
*childer = 15;
can be used for assignment.
you could dereference the name of the array, but just trying to do
childer = -999;
is completely wrong...
see the example below:
int main() {
int childer[] = { 10, 30, 50 };
cout << childer[0] << endl;
cout << *childer << endl;
//now write
childer[0] = 200;
cout << childer[0] << endl;
cout << *childer << endl;
//now write again
*childer = -999;
cout << childer[0] << endl;
cout << *childer << endl;
return 0;
}
First of all
int childer[] = { 10, 20, 30 };
...
childer = ....; <--- THIS WILL NOT COMPILE, as childer is not an lvalue.
you cannot say childer = 20;, you can say childer[0] = 20; or *childer = 20;, or even more *(childer + 1) = 20; (to access other elements than the first), but you cannot use childer as the whole array in an assignment. Simply, neither C nor C++ allow this.
Arrays and pointers are very different things. What confuses you, is that neither C, nor C++ have a way to reference the array as a whole, and the array identifier, by itself, means the address of the first element (and not a pointer but a reference). What I mean by a reference, and I don't use the word pointer for just now, is that a pointer normally refers to a variable of pointer type, and as a variable, can be modified (mainly because the array position cannot be changed at runtime). You cannot write something like array = something_else;, when array is an array. Array names cannot be used by themselves as lvalues, only as rvalues. This can be done with pointers, like
int childer[] = { 10, 20, 30, };
int *pointer = childer;
and then
pointer = some_other_place_to_point_to;
because pointer is a pointer and childer isn't.
The meaning that both languages attribute to the array name is just a pointer rvalue pointing to the address of the first element (and typed as the cell type, continue reading for an explanation on this). And this is not the same as the address of the array (both are different things, demonstrated below) as the first (the array name is a pointer to cell type, while the array address &childer is a pointer to the whole array type). This can be illustrated with the next program, which tries to show you the difference in sizes for the array element, the whole array, the array pointer and the cell pointer:
#include <stdio.h>
#include <sys/types.h>
#define D(x) __FILE__":%d:%s: " x, __LINE__, __func__
#define P(fmt, exp) do { \
printf(D(#exp " == " fmt "\n"), (exp)); \
} while(0)
#define TR(sent) \
sent; \
printf(D(#sent ";\n"))
int main()
{
int i = 1;
TR(double a[10]);
P("%p", a);
P("%p", &a);
P("%d", sizeof a);
P("%d", sizeof &a);
P("%d", sizeof *a);
P("%d", sizeof *&a);
return 0;
}
which executes into (I selected double as the type of cell to make evident the differences while trying to be more architecture independent):
$ pru
pru.c:16:main: double a[10]; <-- array of ten double's (a double is 8 bytes in many architectures)
pru.c:18:main: a == 0xbfbfe798 <-- address values are
pru.c:19:main: &a == 0xbfbfe798 <-- equal for both.
pru.c:20:main: sizeof a == 80 <-- size of whole array.
pru.c:21:main: sizeof &a == 4 <-- size of array address (that's a pointer to array)
pru.c:22:main: sizeof *a == 8 <-- size of pointed cell (first array element)
pru.c:23:main: sizeof *&a == 80 <-- size of pointed array.
But you are wrong when saying that you cannot use an expression to access the other elements, as you can, use array + 1 to mean the address of the second element, and array + n to access the address of the n + 1 element. Evidently array + 0 is the same as array (the 0 number is also neutral in pointer addition), so you can get on it easily. Some people like to write it for fun also as 0+array (to abuse of the commutativity of the + operator) and go further, to write something like 0[array], to refer to the first element of the array (this time the access being to the element, and not to its address) I don't know if this continues to be valid with the new standards, but I'm afraid it continues, perhaps, to allow for legacy code to be compilable (adding to the controversy of what should be conserved and what can be eliminated on standard revisions)

How are 2-Dimensional Arrays stored in memory?

#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[101][101];
a[2][0]=10;
cout<<a+2<<endl;
cout<<*(a+2)<<endl;
cout<<*(*(a+2));
return 0;
}
Why are the values of a+2 and *(a+2) same? Thanks in advance!
a is a 2D array, that means an array of arrays. But it decays to a pointer to an array when used in appropriate context. So:
in a+2, a decays to a pointer to int arrays of size 101. When you pass is to an ostream, you get the address of the first element of this array, that is &(a[2][0])
in *(a+2) is by definition a[2]: it is an array of size 101 that starts at a[2][0]. It decays to a pointer to int, and when you pass it to an ostream you get the address of its first element, that is still &(a[2][0])
**(a+2) is by definition a[2][0]. When you pass it to an ostream you get its int value, here 10.
But beware: a + 2 and a[2] are both pointers to the same address (static_cast<void *>(a+2) is the same as static_cast<void *>(a[2])), but they are pointers to different types: first points to int array of size 101, latter to int.
I'll try to explain you how the memory is mapped by the compiler:
Let's consider a more pratical example multi-dimentional array:
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
You can execute the command
x/10w a
In GDB and look at the memory:
0x7fffffffe750: 1 2 3 4
0x7fffffffe760: 5 6 7 8
0x7fffffffe770: 9 0
Each element is stored in a int type (32 bit / 4 bytes).
So the first element of the matrix has been stored in:
1) a[0][0] -> 0x7fffffffe750
2) a[0][1] -> 0x7fffffffe754
3) a[0][2] -> 0x7fffffffe758
4) a[1][0] -> 0x7fffffffe75c
5) a[1][1] -> 0x7fffffffe760
6) a[1][2] -> 0x7fffffffe764
7) a[2][0] -> 0x7fffffffe768
...
The command:
std::cout << a + 2 << '\n'
It will print the address 0x7fffffffe768 because of the
pointer aritmetic:
Type of a is int** so it's a pointer to pointers.
a+2 is the a[0] (the first row) + 2. The result is a pointer
to the third row.
*(a+2) deferences the third row, that's {7,8,9}
The third row is an array of int, that's a pointer to int.
Then the operator<< will print the value of that pointer.
A 2-dimensional array is an array of arrays, so it's stored like this in memory:
char v[2][3] = {{1,3,5},{5,10,2}};
Content: | 1 | 3 | 5 | 5 | 10 | 2
Address: v v+1 v+2 v+3 v+4 v+5
To access v[x][y], the compiler rewrites it as: *(v + y * M + x) (where M is the second dimension specified)
For example, to access v[1][1], the compiler rewrites it as *(v + 1*3 + 1) => *(v + 4)
Be aware that this is not the same as a pointer to a pointer (char**).
A pointer to a pointer is not an array: it contains and address to a memory cell, which contains another address.
To access a member of a 2-dimensional array using a pointer to a pointer, this is what is done:
char **p;
/* Initialize it */
char c = p[3][5];
Go to the address specified by the content of p;
Add the offset to that address (3 in our case);
Go to that address and get its content (our new address).
Add the second offset to that new address (5 in our case).
Get the content of that address.
While to access the member via a traditional 2-dimensional array, these are the steps:
char p[10][10];
char c = p[3][5];
Get the address of pand sum the first offset (3), multiplied by the dimension of a row (10).
Add the the second offset (5) to the result.
Get the content of that address.
If you have an array like this
T a[N];
then the name of the array is implicitly converted to pointer to its first element with rare exceptions (as for example using an array name in the sizeof operator).
So for example in expression ( a + 2 ) a is converted type T * with value &a[0].
Relative to your example wuth array
int a[101][101];
in expression
a + 2
a is converted to rvalue of type int ( * )[101] and points to the first "row" of the array. a + 2 points to the third "row" of the array. The type of the row is int[101]
Expression *(a+2) gives this third row that has type int[101] that is an array. And this array as it is used in an expression in turn is converted to pointer to its first element of type int *.
It is the same starting address of the memory area occupied by the third row.
Only expression ( a + 2 ) has type int ( * )[101] while expression *( a + 2 ) has type int *. But the both yield the same value - starting address of the memory area occupied by the third row of the array a.
The first element of an array is at the same location as the array itself - there is no "empty space" in an array.
In cout << a + 2, a is implicitly converted into a pointer to its first element, &a[0], and a + 2 is the location of a's third element, &a[2].
In cout << *(a + 2), the array *(a + 2) - that is, a[2] - is converted into a pointer to its first element, &a[2][0].
Since the location of the third element of a and the location of the first element of the third element of a are the same, the output is the same.

Pointer syntax usage in Array

I have some problem understanding the pointers syntax usage in context with two dimensional arrays, though I am comfortable with 1-D array notation and pointers, below is one of the syntax and I am not able to understand how the following expression is evaluated.
To access the element stored at third row second column of array a we will use the subscripted notation as a[2][1] other way to access the same element is
*(a[2]+1)
and if we want to use it as pointers we will do like this
*(*(a+2)+1)
Though I am able to understand the replacement of *(a[2]+1)
as *(*(a+2)+1) but I don't know how this is getting evaluated.
Please explain with example if possible.
Assume array is stored in row wise order and contain the following elements
int a[5][2]={
21,22,
31,32
41,42,
51,52,
61,62
};
and base address of array is 100(just assume) so the address of a[2] is 108 (size of int =2(another assumption)) So the expression *(*(a+2)+1). How does it gets evaluated does it start from the inside bracket and if it does then after the first bracket we have the value to which 1 is being added rather than the address... :/
To start of with
a[i] = *(a+i);
So
a[i][j] = *(a[i] +j)
and
a[i][j] = *(*(a+i) + j)
How a[i] = *(a+i):
If a is a array then the starting address of the array is given by &a[0] or just a
So when you specify
a[i] this will decay to a pointer operation *(a+i) which is, start at the location a and dereference the pointer to get the value stored in the location.
If the memory location is a then the value stored in it is given by *a.
Similarly the address of the next element in the array is given by
&a[1] = (a+1); /* Array decays to a pointer */
Now the location where the element is stored in given by &a[1] or (a+1) so the value stored in that location is given by *(&a[1]) or *(a+1)
For Eg:
int a[3];
They are stored in the memory as shown below:
a a+1 a+2
------------------
| 100 | 102 | 104|
------------------
&a[0] &a[1] &a[2]
Now a is pointing to the first element of the array. If you know the pointer operations a+1 will give you the next location and so on.
In 2D arrays the below is what the access is like :
int arr[m][n];
arr:
will be pointer to first sub array, not the first element of first sub
array, according to relationship of array & pointer, it also represent
the array itself,
arr+1:
will be pointer to second sub array, not the second element of first sub
array,
*(arr+1):
will be pointer to first element of second sub array,
according to relationship of array & pointer, it also represent second
sub array, same as arr[1],
*(arr+1)+2:
will be pointer to third element of second sub array,
*(*(arr+1)+2):
will get value of third element of second sub array,
same as arr[1][2],
A 2D array is actually a consecutive piece of memory. Let me take a example : int a[3][4] is representend in memory by a unique sequence of 12 integer :
a00 a01 a02 a03 a04 a10 a11 a12 a13 a14 a20 a21 a22 a23 a24
| | |
first row second row third row
(of course it can be extended to any muti-dimensional array)
a is an array of int[4] : it decays to a pointer to int[4] (in fact, it decays to &(a[0]))
a[1] is second row. It decays to a int * pointing to beginning of first row.
Even if arrays are not pointer, the fact that they decay to pointers allows to use them in pointer arithmetic : a + 1 is a pointer to second element of array a.
All that explains why a[1] == *(a + 1)
The same reasoning can be applied to a[i] == *(a+i), and from there to all the expressions in your question.
Let's look specifically to *(*(a+2)+1). As said above, a + 2 is a pointer to the third element of an array of int 4. So *(a + 2) is third row, is an array of int[4] and decays to a int * : &(a[2][0]).
As *(a + 2) decays to an int *, we can still use it as a base for pointer arithmetic and *(a + 2) + 1 is a pointer to the second element of third row : *(a + 2) + 1 == &(a[2][1]). Just dereference all that and we get
*(*(a + 2) + 1) == a[2][1]