简体   繁体   中英

How do I return the indices of a multidimensional array element in C?

Say I have a 2D array of random boolean ones and zeroes called 'lattice', and I have a 1D array called 'list' which lists the addresses of all the zeroes in the 2D array. This is how the arrays are defined:

define n 100  
bool lattice[n][n];  
bool *list[n*n];

After filling the lattice with ones and zeroes, I store the addresses of the zeroes in list:

for(j = 0; j < n; j++)
{   
    for(i = 0; i < n; i++)
    {
        if(!lattice[i][j])  // if element = 0
        {
            list[site_num] = &lattice[i][j];  // store address of zero
            site_num++;
        }
    }
}

How do I extract the x,y coordinates of each zero in the array? In other words, is there a way to return the indices of an array element through referring to its address?

EDIT: I need to make the code as efficient as possible, as I'm doing lots of other complicated stuff with much larger arrays. So a fast way of accomplishing this would be great

One solution is to map (x, y) to a natural number (say z ).

z = N * x + y
x = z / N (integer division)
y = z % N

In this case, you should use int list[N * N];

Another solution is to just store the coordinates when you find a zero, something like:

list_x[site_num] = x;
list_y[site_num] = y;
site_num++;

Or you can define a struct of two int s.

Well, it is possible with some pointer arithmetic.

You have the address of your first element of lattice and the addresses of all zero-fields in list. You know the size of bool. By subtracting the first-elements address from a zero-field address and dividing by the size of bool you get a linar index. This linear index can be calculated into the 2-dim index by using modulo and division.

But why don't you store the 2-dim index within your list instead of the address? Do you need the addess or just the index?
And you should think about turning the for-loops around (outer loop i, inner loop j).

How do I extract the x,y coordinates of each zero in the array? In other words, is there a way to return the indices of an array element through referring to its address?
You can't. Simple as that. If you need that information you need to pass it along with the arrays in question.

bool *list[n*n]; is an illegal statement in C89 (EDIT: Unless you made na macro (yuck!)), you may wish to note that variable length arrays are a C99 feature.

struct ListCoords
{
   int x, y;
} coords[n*n];

for(i = 0; i < site_num; i++)
{
    int index = list[i] - &lattice[0][0];
    coords[i].x = index % n;
    coords[i].y = index / n;
}

I may have the % and / operators backwards for your needs, but this should give you the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM