Reputation:
int a[A][B];
int* p = a[i]; //i<A-1
then what's the actual operation of the sentence below?
p++;
p+=B;
Upvotes: 1
Views: 492
Reputation: 1726
This is sort of a meta-answer, aimed at much at Justicle as at the original poster.
Arrays degenerate into pointers, but they are not the same thing. Just because the syntax to get at a[3][7][1] is the same if a is a pointer-to-pointer-to-pointer-to-int versus an array-of-array-of-array-of-int doesn't mean that the actual operation is the same.
You can make an array of any type (of well defined size). Array dereference syntax and pointer syntax are the same, such that
a[i] == *(a+i) == i[a]
no matter what array type a is.
All the answers to questions in this thread can be derived from this.
int a[3][2][17]; //a is an array of length 17.
int (*b)[3][2] = a[5]; //b is a pointer to the fifth element of a
int (*c)[3] = b[1]; //c points to the first element of b.
b += 1; // b now points to the sixth element of a. (c is unchanged)
c += sizeof(*b); // c points to the first element of b again.
Note that none of this would work if a were not contiguous. If it didn't work then arrays of arrays would work differently than arrays of anything else.
Upvotes: 0
Reputation: 98398
After the initialization, a has storage for AB ints and p points to the Bi'th one (0-based). The later operations make it point to the B*(i+1)+1'th one.
Upvotes: 0
Reputation: 71
Just to further the conversation (although I can't actually respond to cbailey's post directly without enough reps...)
There's a big difference between declaring a multi-dimensional array on the stack, and on the heap.
As abelenky might be getting at, declaring a multi-dimensional array on the stack will always result in contiguous memory. I believe that the C standard actually states this somewhere, but even if it doesn't, every compiler on the planet will ensure that it is contiguous. Meaning that at the end of the day, you might as well be using a single dimension array, because the compiler will convert to that anyways.
In terms of being safe when using multi-dimensional arrays, regardless of whether they are allocated on the heap or the stack, you'd not want to iterate over them the way the original poster did.
Upvotes: 0
Reputation: 1294
To answer this question you first need to understand how multi-dimensional arrays are stored in memory in C.
int a[A][B];
Without considering optimizations as to how it sits in memory, the first index (A) will actually be an array of pointers to the second index (B). While the memory may be contiguous, there is no guarantee.
Therefore:
int a[A][B];
int **b;
The difference between a and b above is that the memory for a is allocated when it comes into scope.
With that in mind, to answer your question:
int* p = a[i]; // p = a[i][0]
p++; // p = a[i][1]
p+=B; // p = a[i][1+B]
With a modern compiler it is likely a single contiguous block of memory has been allocated, giving you the equivalent of:
p = a[i+1][1];
But there is no guarantee of the memory being contiguous, so a possible scenario is that you now have an invalid pointer.
Upvotes: -1
Reputation: 2439
For multidimensional arrays, one has to remember that the dimensions are applied from left to right, which makes it somewhat easier to visualise. for example:
int p[A][B][C];
This will be built as :
[][]--[][][][]--[][]_ _ _[][]--[][] [][]--[][][][]--[][]_ _ _[][]--[][]
|---A----||---A----| |---A----|---|---A----||---A----| |---A----|
|---------------A*B---------------| |---------------A*B---------------|
|---------------------------------A*B*C---------------------------------|
so if you have a p[i][j][k], i'ts actually (p+i+Bj+BCk)
Upvotes: 4
Reputation: 1107
p = a[A-1]
is the same as
p = &a[A-1][0]
so
p++
results in
p == &a[A-1][1]
and then doing
p+=B
will result in
p == &a[A-1][1+B] == &a[A][1] // outside allocated memory
Upvotes: 0
Reputation: 2001
The answer is quite simple if you draw the a[A][B] matrix in memory form, aka a vector and start with p pointing at the beginning of a[i] and then apply those operations.
Upvotes: -1
Reputation: 73473
p++
-> Go to next column in the matrix
p+=B
-> Go to the next row (at the same column) in the matrix.
Upvotes: 13