G
gdotone
I'm reading "A Book on C", 4th edition. The author gives the following example when discussing two dimensional arrays:
a[3][5], so let a[][] be of type int.
col 1 col 2 col 3 col 4 col 5
row 1 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
row 2 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
row 3 a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]
The author then says that the following expression are equivalent
a[j]
*(a + j)
(*(a + i))[j]
*((*(a+i)) + j)
*(&a[0][0] + 5*i + j)
I understand that the table is just a convenient representation of the 2D array.
In memory the 2D array is actually contiguous. OK.
My question has to do with saying or writing out in words what is being shown when using the *, dereferencing, and &, address operators.
Is this the correct way of saying:
*(a + j) the base address is pointed to by a and we move along the contiguous memory j (interval), so we add j, j of size int, to get the value at that location we use the dereference operator.
I believe I understand what's happening, but am I saying it right.
(*(a + i))[j] the base address is at a, i is added to the base address, ok, I understand that we are looking at the row location here, but I'm stuck at how to say this.
let's say addresses are given in decimal, an integer in one byte and each address points to a byte. I hope I'm saying this right?
at address 100, a[0][0]
at address 109, a[1][0]
(if this thought is wrong please clear this up)
let's also say we are starting at location 100 (decimal).
consider a[1][0]. row 2, col 1 for the following discussion please.
(*(a + i ))[j] --- (*(a + 1))[0]
so, a, the start of the array is pointing to location 100
What does it mean to add 1 to a?
I mean, I understand that we are now at the address of the second row. I guess this should be known from the additional [0] indicating this is not just a one byte move?
And what about the others like: *((*(a+i)) + j)
Let me stop here, I think I'm starting to confuse myself. (ok, yes i'm already confused.
)
Thanks for the help everyone,
g.
a[3][5], so let a[][] be of type int.
col 1 col 2 col 3 col 4 col 5
row 1 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
row 2 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
row 3 a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]
The author then says that the following expression are equivalent
a[j]
*(a + j)
(*(a + i))[j]
*((*(a+i)) + j)
*(&a[0][0] + 5*i + j)
I understand that the table is just a convenient representation of the 2D array.
In memory the 2D array is actually contiguous. OK.
My question has to do with saying or writing out in words what is being shown when using the *, dereferencing, and &, address operators.
Is this the correct way of saying:
*(a + j) the base address is pointed to by a and we move along the contiguous memory j (interval), so we add j, j of size int, to get the value at that location we use the dereference operator.
I believe I understand what's happening, but am I saying it right.
(*(a + i))[j] the base address is at a, i is added to the base address, ok, I understand that we are looking at the row location here, but I'm stuck at how to say this.
let's say addresses are given in decimal, an integer in one byte and each address points to a byte. I hope I'm saying this right?
at address 100, a[0][0]
at address 109, a[1][0]
(if this thought is wrong please clear this up)
let's also say we are starting at location 100 (decimal).
consider a[1][0]. row 2, col 1 for the following discussion please.
(*(a + i ))[j] --- (*(a + 1))[0]
so, a, the start of the array is pointing to location 100
What does it mean to add 1 to a?
I mean, I understand that we are now at the address of the second row. I guess this should be known from the additional [0] indicating this is not just a one byte move?
And what about the others like: *((*(a+i)) + j)
Let me stop here, I think I'm starting to confuse myself. (ok, yes i'm already confused.
Thanks for the help everyone,
g.