返回 VLA 和用法 [英] Return VLA and usage

查看:102
本文介绍了返回 VLA 和用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

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,类型是什么,如何将其编写为函数签名,以及如何在接收返回数组的地方使用它?

My question is: How do I return the VLA from that function, what is the type and how do I write that as a function signature and how do I use it where I receive the returned array?

目前我正在尝试这样做:

At the moment I am trying to do it like this:

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我收到以下错误:

I tried to use the code suggested by alk, but it gives me a lot of errors while compiling. Here is a separate programm, which contains your suggested code with a main function: http://pastebin.com/R6hKgvM0 I get the following errors:

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;
      ^

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

The errors mainly seem to be about the return type. How do I write the type of that array correctly?

推荐答案

  • 参考 1st 警告:

    warning: initialization from incompatible pointer type
    

    这里

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

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

    参考第二个nd警告:

    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] 也不一样.

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

    I propose you change the function like this:

    #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;
    }
    

    这篇关于返回 VLA 和用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆