Reputation: 73
How would I uses pointers in a multidimensional array? In each direction how would I replace what I have done with pointer arithmetic? I have defined my ptr as *location. I think I need to make this change because I am getting segmentation faults when totalHops>400. Thus, explicitly changing x, y, z each time must be causing this error. Context: I am moving a particle in an L by L by L 3D space. I have a random number generator to determine whether the particle moves left, right, up, down, back or forth each time the particle randomly moves location. (Note I have designed the system to have periodic boundary conditions).
const int L = 10;
int N = L*L*L;
const int totalHops = 200;
int sites[L][L][L] = {};
int x = 0, y = 0, z = 0;
int tracker[N] = {};
int *location;
location = &sites[0][0][0];
for (int i = 1; i <= totalHops; i++) // the random walk //
{
int direction = randomInt(6); // six possible directions to move //
// along x //
if (direction == 0) { // move in negative direction //
x -= 1;
if (x == -1)
{
x = L-1;
}
}
if (direction == 1) { // move in positive direction //
x +=1;
if (x == L)
{
x = 0;
}
}
// along y //
if (direction == 2) { // move in negative direction //
y -= 1;
if (y == -1)
{
y = L-1;
}
}
if (direction == 3) { // move in positive direction //
y +=1;
if (y == L)
{
y = 0;
}
}
// along z //
if (direction == 4) { // move in negative direction //
z -= 1;
if (z == -1)
{
z = L-1;
}
}
if (direction == 5) { // move in positive direction //
z +=1;
if (z == L)
{
z = 0;
}
}
tracker[i] = sites[x][y][z]; }
Many thanks for your help in advance.
Upvotes: 1
Views: 240
Reputation: 23218
Keep in mind, that although C
accommodates array notation such as 2D, 3D, ..., nD arrays to make working with them more natural from a human readability perspective. But in memory, arrays are actually created as a single block of contiguous memory. For example your array:
const int L = 10;
...
int sites[L][L][L] = {0}; //(using {0} is an idiomatic way to init. arrays to all 0
Is arranged in memory as 10*10*10 sequential sizeof(int) sections of memory, starting at the memory location pointed to by sites
.
| | | | | | | | | ...| | | |
^ ^
sites + 0 sites + (10*10*10 - 1)*sizeof(int)
Because of this fact, pointer math becomes pretty straight forward:
*(sites + 0) is equivalent to sites[0][0][0]
*(sites + 1) is equivalent to sites[0][0][1]
*(sites + 2) is equivalent to sites[0][0][2]
...
*(sites + 10) is equivalent to sites[0][1][0]
...
*(sites + 100) is equivalent to sites[1][0][0]
...
*(sites + 998) is equivalent to sites[9][9][8]
*(sites + 999) is equivalent to sites[9][9][9]
The pattern between the pointer notation and array notation becomes very apparent, as the number added to the beginning of the array correlates with the arrangement of indexes in the array notation.
Based on this basic form, you can derive a way to use pointer math to represent multidimensional arrays, in your case then, using int *location;
initialized to the beginning of sites
can be used to track (or determine) what element of the 3D
array is being looked at, or modified.
This could apply nicely to your specific problem in that tracking totalHops
, and making decisions based on values outside the range of 0 - 9
in any direction for x,y,z
may be more difficult than making decisions based on notation such as *(sites + 400)
(per your description in OP).
Upvotes: 2
Reputation: 73
So in conclusion to my segmentation fault, I have simply used the wrong variable in my tracker array. But nonetheless, as a fairly new programmer it was good to have thse conversations and thank you for all of your help! I am glad to have explored both the uses of indexing and pointers.
Array needed to be
int tracker[totalHops] = {};
Upvotes: 0