1/33
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What does the following array decay to?
int a[3][4]
It decays to a pointer that looks like: a(*)[4]
Given:
int m[2][3] = {
{10, 20, 30},
{40, 50, 60},
};
int (*p)[3] = m;What is the meaning and type of p?
p is of type int (*)[3]
p actually points to m[0], which is the entire first row.
Given:
int m[2][3] = {
{10, 20, 30},
{40, 50, 60},
};
int (*p)[3] = m;What does p+1 mean?
If p points to m[0], then p+1 points to m[1].
Given:
int m[2][3] = {
{10, 20, 30},
{40, 50, 60},
};
int (*p)[3] = m;What does *(p+1) mean?
m[1][0]
Since this is a 2D array, it no longer has the same meaning as in a 1D array. p refers to m[0], so adding 1 to it would result in m[1]. But since m[1] represents an entire row, it decays to a pointer to
m[1][0]. This happens because we dereferenced it.
Given:
int m[2][3] = {
{10, 20, 30},
{40, 50, 60},
};
int (*p)[3] = m;What does *(p+1)+2 mean?
m[1][2]
Since this is a 2D array, it no longer has the same meaning as in a 1D array. Here, p+1 refers to m[1]. But since m[1] is referring to an entire row, and it is part of an expression, it decays to a pointer to the very first element in the array, so m[1][0]. When you add 2 to this, it gives you: m[1][2]
Given:
int m[2][3] = {
{10, 20, 30},
{40, 50, 60},
};
int (*p)[3] = m;What does *(*(p+1)+2) mean?
Answer: 60
p represents m[0]
p+1 represents m[1]
*(p+1) represents m[1][0]
*(p+1)+2 represents m[1][2]
therefore: *(*(p+1)+2)represents the value at m[1][2], which is 60.
Given:
float grid[3][2] = {
{1.1, 1.2},
{2.1, 2.2},
{3.1, 3.2}
};Interpret: *(*(q+2)+1)
Answer: 3.2
q points to grid[0]
q+2 points to grid[2]
*(q+2) points to grid[2][0]
*(q+2)+1 points to grid [2][1]
*(*(q+2)+1)means get the value at grid[2][1] = 3.2
Given:
int a[4]= {5,6,7,8};
int *r = a;What does r mean?
r points to a[0]
Given:
int a[4]= {5,6,7,8};
int *r = a;What does r+2 mean?
r points to a[0]
so r+2 means a[2]
Given:
int a[4]= {5,6,7,8};
int *r = a;What does *(r+2) mean?
It means get the value at a[2] = 7
Given:
int a[4]= {5,6,7,8};
int *r = a;What does *r+2 mean?
Answer: 7
It means dereference a[0], and add 2 to that value: 5+2 = 7
Given the following:
int box[2][3][4];
int (*s)[3][4] = &box[0];What does s mean?
s is a pointer to box[0]
Given the following:
int box[2][3][4];
int (*s)[3][4] = &box[0];What does *s mean?
s tries to dereference box[0], but since box[0] actually represents an entire row, it actually just decays to a pointer to the very first element in that row: box[0][0].
Given the following:
int box[2][3][4];
int (*s)[3][4] = &box[0];What does (*s)[1] mean?
Answer: box[0][1]
s refers to box[0]
*s refers to box[0][0] (because the array decays
therefore: (*s)[1] refers to box[0][1]
Given the following:
int box[2][3][4];
int (*s)[3][4] = &box[0];What does (*s)[1]+2 mean?
Answer: box[0][1][2]
s refers to box[0]
*s refers to box[0][0]
(*s)[1] refers to box[0][1]
(*s)[1]+2 refers to box[0][1][2]
Given the following:
int box[2][3][4];
int (*s)[3][4] = &box[0];What does *((*s)[1]+2) mean?
Answer: box[0][1][2]
s refers to box[0]
*s refers to box[0][0]
(*s)[1] refers to box[0][1]
(*s)[1]+2 refers to box[0][1][2]
*((*s)[1]+2)refers to the value at box[0][1][2]
Given
int A[4][2] = {
{1,2},
{3,4},
{5,6},
{7,8}
};
int (*p)[2] = A;Interpret: *(*(p+2)+1)
Answer: 6
p represents A[0]
p+2 represents A[2]
*(p+2) represents A[2][0]
(*(p+2)+1)represents A[2][1]
*(*(p+2)+1)means get the value at A[2][1] = 6
Given:
double B[3][3] = {
{1.1, 1.2, 1.3},
{2.1, 2.2, 2.3},
{3.1, 3.2, 3.3}
};
double *q = &B[1][0]Interpret q
q is a pointer to B[1][0] because that is what we specified in the original code.
Given:
double B[3][3] = {
{1.1, 1.2, 1.3},
{2.1, 2.2, 2.3},
{3.1, 3.2, 3.3}
};
double *q = &B[1][0]Interpret q+1
Answer: B[1][1]
q refers to B[1][0]
q+1 refers to B[1][1]
Given:
double B[3][3] = {
{1.1, 1.2, 1.3},
{2.1, 2.2, 2.3},
{3.1, 3.2, 3.3}
};
double *q = &B[1][0]Interpret *(q+1)
Answer: 2.2
q points to B[1][0]
q+1 points to B[1][1]
*(q+1) is the value at B[1][1] = 2.2
Given:
double B[3][3] = {
{1.1, 1.2, 1.3},
{2.1, 2.2, 2.3},
{3.1, 3.2, 3.3}
};
double *q = &B[1][0]Interpret *(q+3)
Answer: 3.1
q points to B[1][0]
q+3 points to B[1][3], but since this is out of bounds, it automatically moves to the next element: B[2][0]
*(q+3) gets the value at B[2][0] = 3.1
Given:
double B[3][3] = {
{1.1, 1.2, 1.3},
{2.1, 2.2, 2.3},
{3.1, 3.2, 3.3}
};
double *q = &B[1][0]Interpret *(q+4)
Answer: 3.2
q points to B[1][0]
q+4 points to B[1][4]
*(q+4) gets the value at B[1][4], but since this is out of bounds, it actually refers to B[2][1] = 3.2
Given:
int C[2][2][3] = {
{ {10,11,12}, {13,14,15} },
{ {20,21,22}, {23,24,25} }
};
int (*r)[2][3] = &C[1];Interpret: *((*r)[0]+2)
Answer: 22
r points to C[1]
*r points to C[1][0] (because it decays to a pointer)
(*r)[0]points to C[1][0][0]
((*r)[0]+2)points to C[1][0][2]
*((*r)[0]+2)gets the value at C[1][0][2], which is 22
Given:
int D[3][4][2];
int (*t)[2] = D[2];Evaluate: *(*(t+1)+1)
Answer: gets the value at D[2][1][1]
t points to D[2]
t+1 points to D[2][1]
*(t+1) points to D[2][1][0]
*(t+1)+1points to D[2][1][1]
*(*(t+1)+1)gets the value at D[2][1][1]
Note: +1 only adds a brand new bracket like [1] if it is currently at the final dimension of the array, otherwise, it just adds one to the current bracket, like if we are at [0], it will make it [1]
Given:
float E[2][3] = {
{9.1, 9.2, 9.3},
{8.1, 8.2, 8.3}
};
float (*u)[3] = &E[0];What does u point to?
u points to E[0]
Given:
float E[2][3] = {
{9.1, 9.2, 9.3},
{8.1, 8.2, 8.3}
};
float (*u)[3] = &E[0];What does *u point to?
Answer: E[0][0]
u points to E[0]
*u points to E[0][0]
Given:
float E[2][3] = {
{9.1, 9.2, 9.3},
{8.1, 8.2, 8.3}
};
float (*u)[3] = &E[0];What does (*u)+2 point to?
Answer: E[0][2]
u points to E[0]
*u points to E[0][0]
(*u)+2 points to E[0][2]
Given:
float E[2][3] = {
{9.1, 9.2, 9.3},
{8.1, 8.2, 8.3}
};
float (*u)[3] = &E[0];What does *((*u)+2) evaluate to?
Answer: 9.3
u points to E[0]
*u points to E[0][0]
(*u)+2 points to E[0][2]
*((*u)+2)gets the value at E[0][2] = 9.3
Given:
float E[2][3] = {
{9.1, 9.2, 9.3},
{8.1, 8.2, 8.3}
};
float (*u)[3] = &E[0];What does *(u+1) evaluate to?
Answer: points to E[1][0]
u points to E[0]
u+1 points to E[1]
*u points to E[1][0] (because it decays to a pointer to the first element in the array)
Given:
int F[2][2][2] = {
{ {1,2}, {3,4} },
{ {5,6}, {7,8} }
};
int *w = &F[0][1][0];Evaluate *(w+1)
Answer: 4
w points to F[0][1][0]
w+1 points to F[0][1][1]
*(w+1) gets the value at F[0][1][1] = 4
Given:
int F[2][2][2] = {
{ {1,2}, {3,4} },
{ {5,6}, {7,8} }
};
int *w = &F[0][1][0];Evaluate *(w+5)
Answer: 8
w points to F[0][1][0]
w+5 points to F[0][1][5]
*(w+5) tries to get the value at F[0][1][5], but since this is out of bounds by 3 elements, it actually moves to the next valid element F[0][2][3], but since this also out of bounds, it has to move again to:
F[1][0][3], but this is still out of bounds, so finally, it moves to: F[1][1][1] = 8.
We can also verify this by just counting up to index 5 beginning at F[0][1][1], which will lead us to 8.
Given:
int F[2][2][2] = {
{ {1,2}, {3,4} },
{ {5,6}, {7,8} }
};
int *w = &F[0][1][0];Evaluate: *(w+1)
Answer: 4
w points to F[0][1][0]
w+1 points to F[0][1][1]
*(w+1) gets the value at F[0][1][1] = 4
Given:
int F[2][2][2] = {
{ {1,2}, {3,4} },
{ {5,6}, {7,8} }
};
int *w = &F[0][1][0];Evaluate: *(w+5)
Answer: 8
w points to F[0][1][0]
w+5 points to F[0][1][5], which is out of bounds
*(w+5) moves and gets the next valid value at F[1][1][1] = 8
What happens when you compare memory addresses like &arr[0] < &arr[1]
When you are comparing memory address, you are comparing the actual numerical memory locations stored as the values of the pointers. Luckily, these are stored in ascending order;
This means that:
&arr[0]= 1000
&arr[1]= 1004
&arr[2]= 1008
They are sorted in increasing order. Here, they each increase by 4 bytes since an int is 4 bytes.