简体   繁体   English

二维数组可以这样初始化吗?

[英]Can a 2D array be initialised this way?

/* Initialize matrix with values from 0 to N*N.  */
void
init_matrix_seq (unsigned N, float * m)
{
    unsigned i;

    for (i = 0; i < N*N; ++i)
        m[i] = (float) i;
}

I mean, it looks like one loop is being used to go through N*N elements in one dimension. 我的意思是,看起来好像正在使用一个循环来在一维中遍历N * N个元素。 Is this possible in C, without the need for another loop to cycle through columns? 在C中是否可能这样做,而无需另一个循环遍历列?

EDIT: 编辑:

Code which initialises the 2D arrays and calls this function is shown here: 初始化2D数组并调用此函数的代码如下所示:

  A = (float *) malloc (N * N * sizeof (float));
  B = (float *) malloc (N * N * sizeof (float));

  init_matrix (N, A, 1.0);
  init_matrix (N, B, 1.0);

If all you want is to set them ALL to some initial value, then yes, use a single 1D indexing as such: 如果只需要将ALL设置为某个初始值,那么可以,使用一个一维索引,如下所示:

void init_matrix(unsigned N, float m[], float init_val)
{
    unsigned i;
    for (i = 0; i < N*N; ++i)
        m[i] = init_val;
}

Your sequential initialization would be exactly as you have it in your question: 您的顺序初始化将完全与您在问题中所拥有的一样:

void init_matrix_seq(unsigned N, float m[])
{
    unsigned i;
    for (i = 0; i < N*N; ++i)
        m[i] = i;
}

If you have specific values that need to hit positions m[i][j] then in C you can access by formal row-width by: 如果您有需要击中位置m[i][j]特定值,则在C中,您可以通过以下方式按正式的行宽进行访问:

void init_matrix_seq (unsigned N, float m[][N])
{
    unsigned i,j;

    for (i = 0; i < N; ++i)
        for (j = 0; j < N; ++j)
        {
            m[i][j] = i*j; // <<your initial value here>>;
        }
}

If you want to access it as a single linear array you certainly can. 如果要作为单个线性数组访问它,当然可以。 The following accomplishes the same result as above (with the same assumption this is N*N floats wide). 下面的代码完成与上面相同的结果(在相同的假设下,此宽度为N * N个浮点)。

void init_matrix_seq (unsigned N, float m[])
{
    unsigned i,j;

    for (i = 0; i < N; ++i)
        for (j = 0; j < N; ++j)
        {
            m[i*N+j] = i*j; // <<your initial value here>>;
        }
}

The latter example has the nicety of working in C and C++, the former will only work in C (last I checked, anyway). 后一个示例可以很好地在C和C ++中工作,前一个示例仅可以在C中工作(无论如何,我最后还是检查过)。

The answer is yes, but you can't use the double indexing and you could get issues if you want to index an element, instead of A[i][j] you have to call A[i*N+j]. 答案是肯定的,但是您不能使用双索引,并且如果要索引元素,则可能会遇到问题,而不是A [i] [j],您必须调用A [i * N + j]。
If you want to do this using double indexes and having all adjacent in memory, then allocate the array this way: 如果要使用双索引并在内存中具有所有相邻地址,请按以下方式分配数组:

float (*A) [N]= (float (*)[N] ) malloc(N*sizeof(float[N]) );

Now you have all adjacent in memory, you can use the function that you've written.But you can also use double indexing: 现在您的内存已全部相邻,可以使用已编写的函数,但也可以使用双索引:

for(int i=0; i<N; i++)
    for(int j=0; j<N; j++)
        A[i][j]=i*N+j;

But your method is correct. 但是你的方法是正确的。

You are correct; 你是对的; m is a one-dimensional array. m是一维数组。 Sometimes, people will use a 1D array in place of a 2D array (for the slight speed boost) by assigning each row to a section of the 1D array. 有时,人们会通过将每一行分配给一维数组的一部分来代替一维数组来使用一维数组。 I would not recommend doing this, because it hurts readability and the speed increase is less than trivial. 我不建议您这样做,因为它会损害可读性,并且速度提高的幅度不大。

This sample program tell you the way of using 1D array as 2D array 该示例程序告诉您将1D数组用作2D数组的方式

#include <stdio.h>
#define rows 4
#define cols 6

int main()
{
    int array1D[rows * cols] = 
         { 1, 2, 3, 4, 5, 6,
           7, 8, 9, 10, 11, 12,
           13, 14, 15, 16, 17, 18,
           19, 20, 21, 22, 23, 24 };

    int (*array2D)[cols] = (int (*)[cols]) array1D;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", array2D[i][j] );
        }
        printf("\n");
    }
 }

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

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