简体   繁体   English

从C函数返回指向二维数组的指针

[英]Return a pointer to a 2-D array from a C-function

I know this question has been asked here , here and here and many more times, but something's not working yet. 我知道这个问题在这里这里这里被问很多次了,但是还没有解决。 I am trying to make a karnaugh map solver in C and I want one of my functions to pass a 2D array back to the main function, so here is my program: 我正在尝试用C语言制作一个卡诺地图求解器,我希望我的一个函数将2D数组传递回主函数,所以这是我的程序:

    typedef char (*arr)[][4];

    int rows;

    int main(void){
      int noVar;
      char *grid;
      printf("Please Enter the number of variables: ");


      scanf("%d",&noVar);
      grid = setMap(noVar);
      feedOnes(grid);
    }

    arr setMap(int noVar){

    rows = pow(2,noVar)/4;
    char grid[rows][4];
    for(int i = 0; i<rows; i++){
        for(int j = 0; j<4; j++){
            grid[i][j]='0';
        }
    }
    printGrid(grid);
    return &grid;
}

This gives me an 4 warnings while it does the job for now: 这会给我一个4条警告,提示它目前正在工作:

    In file included from kmap.c:9:
./setMap.h:33:13: warning: address of stack memory associated with local
      variable 'grid' returned [-Wreturn-stack-address]
    return &grid;
            ^~~~
./setMap.h:56:1: warning: control reaches end of non-void function
      [-Wreturn-type]
}
^
kmap.c:16:8: warning: incompatible pointer types assigning to 'char *' from
      'arr' (aka 'char (*)[][4]') [-Wincompatible-pointer-types]
  grid = setMap(noVar);
       ^ ~~~~~~~~~~~~~
kmap.c:17:12: warning: incompatible pointer types passing 'char *' to parameter
      of type 'char (*)[4]' [-Wincompatible-pointer-types]
  feedOnes(grid);
           ^~~~
./setMap.h:36:19: note: passing argument to parameter 'grid' here
int feedOnes(char grid[][4]){
                  ^

My question is, can I resolve these warnings? 我的问题是,我可以解决这些警告吗? and will these warnings cause any problem in future as I do not know why are they appearing 这些警告将来会不会引起任何问题,因为我不知道为什么会出现

Also, I am a newbie so please don't be harsh at me if this question was not asked properly.. 另外,我是新手,所以如果这个问题没有正确提出,请不要对我严厉。

Thank You. 谢谢。

The array grid[][] is local to the setMap() function. 数组grid[][]对于setMap()函数而言是局部的。 Once this function returns, the variable is no longer available and is out-of-scope. 此函数返回后,该变量将不再可用且不在范围内。 Local variables are declared on the stack memory . 局部变量堆栈存储器中声明。

In order for something like this to work you will need to allocate and deallocate memory from the heap using the malloc() and free() function calls respectively. 为了使类似的东西起作用,您将需要分别使用malloc()free()函数调用从堆中 分配释放内存。

Refer to this link for clarification on Stack vs Heap Memory: 请参考此链接以获取有关堆栈与堆内存的说明:

What and where are the stack and heap? 什么是堆栈和堆?

Refer to this link for scoping in C : 请参阅此链接以了解C范围

https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm

Happy Programming! 编程愉快!

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

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