gpt4 book ai didi

c - 返回VLA和使用情况

转载 作者:行者123 更新时间:2023-12-02 21:16:16 25 4
gpt4 key购买 nike

我有以下功能:

int* create_matrix_2(int rows, int cols)
{
double (*A)[rows][cols] = malloc(sizeof(int[rows][cols]));

for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
*A[row][col] = row * cols + col;
}
}

for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
printf("%lf, " , *A[row][col]);
}
printf("\n");
}
return A;
}

我的问题是:如何从该函数返回 VLA,类型是什么,如何将其编写为函数签名,以及如何在接收返回的数组时使用它?

目前我正在尝试这样做:

int (*matrix2)[height][width] = create_matrix_2(height, width);

for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
printf("%d" , (*matrix2)[row][col]);
}
printf("\n");
}

然后运行:gcc 2d_array_test.c -o 2d_array_test.out -std=c99 -O0

但这会导致以下问题:

2d_array_test.c: In function ‘main’:
2d_array_test.c:35:34: warning: initialization from incompatible pointer type [enabled by default]
int (*matrix2)[height][width] = create_matrix_2(height, width);
^
2d_array_test.c: In function ‘create_matrix_2’:
2d_array_test.c:105:2: warning: return from incompatible pointer type [enabled by default]
return A;
^

编辑#1:

我尝试使用alk建议的代码,但编译时出现很多错误。这是一个单独的程序,其中包含您建议的带有主函数的代码:http://pastebin.com/R6hKgvM0我收到以下错误:

2d_array_test_new.c: In function ‘main’:
2d_array_test_new.c:18:2: warning: passing argument 3 of ‘create_matrix’ from incompatible pointer type [enabled by default]
if (-1 == create_matrix(height, width, &matrix))
^
2d_array_test_new.c:10:5: note: expected ‘int **’ but argument is of type ‘int (**)[(sizetype)(height)][(sizetype)(width)]’
int create_matrix(size_t, size_t, int**);
^
2d_array_test_new.c: At top level:
2d_array_test_new.c:37:5: error: conflicting types for ‘create_matrix’
int create_matrix(size_t rows, size_t cols, int(**a)[rows][cols])
^
2d_array_test_new.c:10:5: note: previous declaration of ‘create_matrix’ was here
int create_matrix(size_t, size_t, int**);
^
2d_array_test_new.c: In function ‘create_matrix’:
2d_array_test_new.c:45:11: error: ‘EINVAL’ undeclared (first use in this function)
errno = EINVAL;
^
2d_array_test_new.c:45:11: note: each undeclared identifier is reported only once for each function it appears in
2d_array_test_new.c:40:6: warning: variable ‘errno’ set but not used [-Wunused-but-set-variable]
int errno;
^

错误主要似乎与返回类型有关。如何正确编写该数组的类型?

最佳答案

  • 引用第一个警告:

    warning: initialization from incompatible pointer type

    这里

    int (*matrix2)[height][width] = create_matrix_2(height, width);

    int (*matrix2)[height][width]int * 根本不一样。

  • 引用第二个警告:

    warning: return from incompatible pointer type 

    这归因于定义

    double (*A)[rows][cols] = malloc(sizeof(int[rows][cols]));

    同时从int * create_matrix_2()返回A

    int *double (*A)[rows][cols] 也不相同。

我建议你像这样更改函数:

#include <errno.h> /* for errno and EINVAL */
#include <stdlib.h> /* for malloc() */


int create_matrix_2(size_t rows, size_t cols, int(**a)[rows][cols])
{
int result = 0;

if (NULL == a)
{
result = -1;
errno = EINVAL;
}
else
{
(*a) = malloc(sizeof **a);
if (NULL == (*a))
{
result = -1;
}
else
{
for (size_t row = 0; row < rows; row++)
{
for (size_t col = 0; col < cols; col++)
{
(**a)[row][col] = row * cols + col;
}
}

for (size_t row = 0; row < rows; row++)
{
for (size_t col = 0; col < cols; col++)
{
printf("%d, " , (**a)[row][col]);
}

printf("\n");
}
}
}

return result;
}

并这样调用它:

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


int create_matrix_2(size_t rows, size_t cols, int(**a)[rows][cols]);


int main(void)
{
int result = EXIT_SUCCESS;

int (*matrix2)[height][width] = NULL;
if (-1 == create_matrix_2(height, width, &matrix2))
{
perror("create_matrix_2() failed");
result = EXIT_FAILURE;
}
else
{
for (size_t row = 0; row < height; row++)
{
for (size_t col = 0; col < width; col++)
{
printf("%d, " , (*matrix2)[row][col]);
}

printf("\n");
}

free(matrix2);
}

return result;
}

关于c - 返回VLA和使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com