Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何调用由main之外的函数创建的二维数组?_C_Function - Fatal编程技术网

C 如何调用由main之外的函数创建的二维数组?

C 如何调用由main之外的函数创建的二维数组?,c,function,C,Function,我对标题、回音和呼叫的外观感到困惑 这就是导致错误“指针类型错误”的原因 为什么这不是正确的做法 注意:将很快发布TableCreator要使其生效,p必须是int***类型,或者您需要从作业中删除* 分配指定要将调用TableCreator的结果放入p指向的内存地址TableCreator返回类型为int**的值,并且您在赋值中有一个附加的间接级别。TableCreator返回指向指针的指针。您需要按以下方式调用函数: int **p; p = TableCreator(arg1, arg2)

我对标题、回音和呼叫的外观感到困惑

这就是导致错误“指针类型错误”的原因

为什么这不是正确的做法


注意:将很快发布TableCreator要使其生效,
p
必须是
int***
类型,或者您需要从作业中删除
*


分配指定要将调用
TableCreator
的结果放入
p
指向的内存地址
TableCreator
返回类型为
int**
的值,并且您在赋值中有一个附加的间接级别。

TableCreator返回指向指针的指针。您需要按以下方式调用函数:

int **p;
p = TableCreator(arg1, arg2);

或者其他类似的东西。为了告诉您如何使用
p
,我们需要查看完整的函数TableCreator。

函数TableCreator()返回int**指针,因此p也必须是int**指针。你没有给出完整的代码,所以我不知道p的类型。我认为变量p的类型不对

您的函数有点不正确。首先,您正在堆栈上创建二维数组
table[r][c]
,不能从c中的函数返回堆栈分配的数组

您必须在堆上分配表,并返回该表。下面是一个例子:

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

int** create_table (int r,int c)
{
    // malloc the row pointers
    // in case you're wondering what I'm doing with sizeof(*table),
    // it's getting the size of *table which is the size of the 
    // underlying pointer type, because we will be allocating the 
    // column pointers. C allows this seemingly weird syntax 
    // because sizeof does not evaluate its operand.

    int **table = malloc(r * sizeof(*table));

    // for each row pointer, allocate 'c' column pointers
    for (int i = 0; i < r; ++i) {
        // sizeof(**table) gets the underlying type of a double-dereference,
        // which is sizeof(int) in this case.
        table[i] = malloc(c * sizeof(**table));
    }

    // read into your table
    for (int row = 0; row < r; ++row) {
        for (int col = 0; col < c; ++col) {
            printf("Enter orientation for table[%d][%d]: ", row, col);
            scanf ("%d",&table[0][0]);
        }
    }

    return table;
}

int main() {
    int row = 10;
    int col = 10;
    int **table = create_table(row, col);

    for (int r = 0; r < row; ++r) {
        for (int c = 0; c < col; ++c) {
            printf("table[%d][%d]: %d\n", r, c, table[r][c]);
        }
    }

    // free all the column pointers
    for (int i = 0; i < row; ++i) {
        free(table[i]);
    }

    // free the row pointers
    free(table);

    return 0;
}
#包括
#包括
int**create_表格(int r,int c)
{
//malloc行指针
//如果你想知道我在用sizeof(*表)做什么,
//它得到的是*表的大小,也就是
//底层指针类型,因为我们将分配
//C允许这种看似奇怪的语法
//因为sizeof不计算其操作数。
int**table=malloc(r*sizeof(*table));
//对于每个行指针,分配“c”列指针
对于(int i=0;i
如果您不了解C内存分配和管理,请阅读以下内容:


TableCreator
返回一个双指针,什么是
p
?这里看起来像一个指针。请显示您的完整代码。我们需要查看更多代码。
p
的类型是什么?此外,请尝试使用四个空格缩进代码,使其显示为代码格式。您的标题和问题不符合。。。。!现在您已经向我们展示了表创建代码,我们的猜测结果有点不正确,但我认为我的答案应该会有所帮助。我添加了完整的函数和调用,如果您想查看它的话
int **p;
p = TableCreator(arg1, arg2);
#include <stdio.h>
#include <stdlib.h>

int** create_table (int r,int c)
{
    // malloc the row pointers
    // in case you're wondering what I'm doing with sizeof(*table),
    // it's getting the size of *table which is the size of the 
    // underlying pointer type, because we will be allocating the 
    // column pointers. C allows this seemingly weird syntax 
    // because sizeof does not evaluate its operand.

    int **table = malloc(r * sizeof(*table));

    // for each row pointer, allocate 'c' column pointers
    for (int i = 0; i < r; ++i) {
        // sizeof(**table) gets the underlying type of a double-dereference,
        // which is sizeof(int) in this case.
        table[i] = malloc(c * sizeof(**table));
    }

    // read into your table
    for (int row = 0; row < r; ++row) {
        for (int col = 0; col < c; ++col) {
            printf("Enter orientation for table[%d][%d]: ", row, col);
            scanf ("%d",&table[0][0]);
        }
    }

    return table;
}

int main() {
    int row = 10;
    int col = 10;
    int **table = create_table(row, col);

    for (int r = 0; r < row; ++r) {
        for (int c = 0; c < col; ++c) {
            printf("table[%d][%d]: %d\n", r, c, table[r][c]);
        }
    }

    // free all the column pointers
    for (int i = 0; i < row; ++i) {
        free(table[i]);
    }

    // free the row pointers
    free(table);

    return 0;
}