c - 使用 calloc 分配二维数组

标签 c arrays pointers dynamic-memory-allocation

我正在尝试使用 calloc 动态分配二维数组。

这是我试过的代码。

int **      numberOfConstPiArray = calloc(invariannumberOfUniqueKernels * kernelColumnCount, sizeof(int *));

我已经初始化了如下所示的变量

    int numberOfUniqueKernels = 100;
    int kernelColumnCount = 10;
    int dimensionalMatrixColumnCount = 10;

以下是循环遍历并尝试更改二维数组的主要代码。

for (int countKernel = 0; countKernel < numberOfUniqueKernels; countKernel++)
{
    int countNumberOfConst = 0;
    int numberOfTerms = 0;
    int numberOfConstPi = 0;

    for (int col = 0; col < kernelColumnCount; col++)
    {
        for (int row = 0; row < dimensionalMatrixColumnCount; row++)
        {
            if (some condition is satisfied)
            {
                countNumberOfConst += 1;
            }

            if (another condition satisfied)
            {
                numberOfTerms += 1;
            }

        }

        if(countNumberOfConst == numberOfTerms)
        {
            numberOfConstPi += 1;
            numberOfConstPiArray[countKernel][col] = 1;
        }

        countNumberOfConst=0;
        numberOfTerms=0;
    }


}

这似乎行不通。我知道似乎不起作用是模糊的,但由于这段代码是大型编译器的一部分,我无法打印出具体的输出。对此表示歉意。

我的问题是:我是否以正确的方式初始化了数组,以及我是否以正确的方式修改了数组中元素的值。

谢谢。

最佳答案

这个

int **      numberOfConstPiArray = calloc(invariannumberOfUniqueKernels * kernelColumnCount, sizeof(int *));

不是二维数组的分配,因为至少 numberOfConstPiArray 的类型是 int ** 而不是例如 int ( * )[ kernelColumnCount].

如果您的编译器支持可变长度数组,那么您可以使用演示程序中显示的以下方法

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    size_t n = 5;

    int ( *a )[n] = calloc( n * n, sizeof( int ) );

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) a[i][j] = i * n + j;           
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) printf( "%2d ", a[i][j] );
        putchar( '\n' );
    }

    free( a );

    return 0;
}

程序输出为

 0  1  2  3  4 
 5  6  7  8  9 
10 11 12 13 14 
15 16 17 18 19 
20 21 22 23 24 

或者您可以通过以下方式分配一个数组数组。

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    size_t n = 5;

    int **a = calloc( n, sizeof( int * ) );

    for ( size_t i = 0; i < n; i++ )
    {
        a[i] = calloc( n, sizeof( int ) );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) a[i][j] = i * n + j;           
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) printf( "%2d ", a[i][j] );
        putchar( '\n' );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        free( a[i] );
    }

    free( a );

    return 0;
}

程序输出与上图相同。

关于c - 使用 calloc 分配二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614091/

相关文章:

arrays - Mongo 查询 - 推送唯一项并保留数组中的顺序

android - 在数组列表中搜索

c - 成功调用 fseek() 函数会清除流的文件结束指示器

C:两个void指针的区别是什么类型?

C函数参数优化: (MyStruct const * const myStruct) vs.(MyStruct const myStruct)

iphone - 在 Objective-C 中初始化为类变量时,在哪种内存上分配 C 结构

c - 完全未使用的内存如何碎片化?

c - C 语言递归金字塔星形图案程序

c - 空指针与其他指针类型的兼容性

arrays - 按频率排序