简体   繁体   English

如何在C中返回多维数组元素的索引?

[英]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. 假设我有一个随机布尔布尔值和零的二维数组,称为“ lattice”,还有一个名为“ list”的一维数组,它列出了2D数组中所有零的地址。 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: 用1和0填充晶格后,将0的地址存储在列表中:

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? 如何提取数组中每个零的x,y坐标? 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 ). 一种解决方案是将(x, y)映射到自然数(例如z )。

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

In this case, you should use int list[N * N]; 在这种情况下,您应该使用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. 或者,您可以定义两个int的结构。

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. 可以使用模和除法将该线性索引计算为2维索引。

But why don't you store the 2-dim index within your list instead of the address? 但是,为什么不将2维索引存储在列表中而不是地址中呢? Do you need the addess or just the index? 您需要addess还是仅索引?
And you should think about turning the for-loops around (outer loop i, inner loop j). 您应该考虑扭转for循环(外循环i,内循环j)。

How do I extract the x,y coordinates of each zero in the array? 如何提取数组中每个零的x,y坐标? 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. 是C89中的一条非法声明(编辑:除非您创建了na宏(糟糕!)),否则您可能需要注意可变长度数组是C99的功能。

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. 对于您的需求,我可能会将%和/运算符向后移动,但这应该可以给您带来启发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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