简体   繁体   English

我怎样才能在c中创建一个n维数组

[英]How can i create an n-dimensional array in c

I was thinking of writing a function that takes n parameters and returns an n-dimensional array using those parameters as the dimensions. 我正在考虑编写一个带有n个参数的函数,并使用这些参数作为维度返回一个n维数组。 Now i realize an one-d and 2d array is easy to implement with pointers. 现在我意识到使用指针很容易实现一维和二维数组。 For 2d array the snippet would be something like (standard way) : 对于2d数组,代码片段就像(标准方式):

int** x;
int* temp;

x = (int**)malloc(m * sizeof(int*));
temp = (int*)malloc(m*n * sizeof(int));
for (int i = 0; i < m; i++) {
  x[i] = temp + (i * n);
}

where the array is of size m*n; 数组的大小为m * n; But the problem lies how do we find the nested loop parameters for a n-dimensional array? 但问题在于我们如何找到n维数组的嵌套循环参数? Is there any way to optimize the code? 有没有办法优化代码?

This shows how to create an N-dimensional array and how to index its elements. 这显示了如何创建N维数组以及如何索引其元素。 These provide the basic mechanisms needed. 这些提供了所需的基本机制。 This is something students consider when learning, but it is rarely used in practice. 这是学生在学习时考虑的事情,但在实践中很少使用。 There are usually better ways to organize data structures. 通常有更好的方法来组织数据结构。 Additionally, most useful algorithms would have patterns in how they traverse the data, so it would be better to build code that updates indices efficiently in an incremental manner rather than recalculating them from scratch as shown below. 此外,大多数有用的算法都会有关于它们如何遍历数据的模式,因此最好构建以增量方式有效更新索引的代码,而不是从头开始重新计算它们,如下所示。

/*  Note:  For demonstration purposes only.  Depending on needs, other types
    might be used for indices and sizes, and the array type might be wrapped
    in an opaque struct rather than exposed as "int *".
*/


//  Create an array with N dimensions with sizes specified in D.
int *CreateArray(size_t N, size_t D[])
{
    //  Calculate size needed.
    size_t s = sizeof(int);
    for (size_t n = 0; n < N; ++n)
        s *= D[n];

    //  Allocate space.
    return malloc(s);
}

/*  Return a pointer to an element in an N-dimensional A array with sizes
    specified in D and indices to the particular element specified in I.
*/
int *Element(int *A, size_t N, size_t D[], size_t I[])
{
    //  Handle degenerate case.
    if (N == 0)
        return A;

    //  Map N-dimensional indices to one dimension.
    int index = I[0];
    for (size_t n = 1; n < N; ++n)
        index = index * D[n] + I[n];

    //  Return address of element.
    return &A[index];
}

Example of use: 使用示例:

//  Create a 3*3*7*7*9 array.
size_t Size[5] = { 3, 3, 7, 7, 9 };
int *Array = CreateArray(5, Size);

//  Set element [1][2][3][4][5] to -987.
*Element(Array, 5, Size, (size_t []) { 1, 2, 3, 4, 5 }) = -987;

I prefer not to use multidimensional arrays, use 1D instead: 我不想使用多维数组,而是使用1D:

int* pArray = (int*) malloc(m * n * sizeof(int*));

// access pArray[a][b]
int result = pArray[a*m + b];

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

相关问题 可以使用单个 C 函数序列化 N 维数组吗? - Can an N-dimensional array be serialized with a single C function? 如何在内存中表示n维(n> = 2)数组? - How an n-dimensional (n>=2) array is represented in memory? 如何计算N维数组的元素地址 - How to Calculate the Element Addresses of an N-Dimensional Array 将一维“展平”索引转换为N维数组的N维矢量索引 - Transforming a one-dimensional, “flattened” index into the N-dimensional vector index of an N-dimensional array 为什么必须在c中初始化n维数组时明确指定n-1维度 - Why must n-1 dimensions be specified explicitly when initiailizing an n-dimensional array in c 如何在C中重新排列n维矩阵上的维? (类似于在Matlab中置换(A,[2:n 1])) - How to re order the dimensions on n-dimensional matrix in C? (Similar to permute(A,[2:n 1]) in Matlab ) 在C中,如何编写N维嵌套的for循环,其中N是变量 - In C, how to write a N-dimensional nested for loop where N is variable 在C:n维数组中取消引用非指针 - Dereferencing Non-Pointers in C: n-dimensional arrays 如何创建指向二维数组的指针数组 - How can I create an array of pointers point to a 2 dimensional array 如何在 C 中创建一维字符串数组? - How do I create an one-dimensional array of strings in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM