Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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-返回指向2d数组的指针_C_Arrays - Fatal编程技术网

C-返回指向2d数组的指针

C-返回指向2d数组的指针,c,arrays,C,Arrays,我正在尝试为结构的2d数组编写一个getter。我尝试过各种各样的解决方案,我的IDE对它们都有抱怨 static history_t history[3][6]; history_t **get_history(){ return history; }; 据我理解,这是正确的。数组的数组是指向指针的指针。但是,我的IDE抱怨指针类型不兼容。我无法找到任何签名和历史访问器的组合,这些组合允许我返回任何有用的内容 这在C语言中是可能的吗 warning: returning 'hist

我正在尝试为结构的2d数组编写一个getter。我尝试过各种各样的解决方案,我的IDE对它们都有抱怨

static history_t history[3][6];

history_t **get_history(){
    return history;
};
据我理解,这是正确的。数组的数组是指向指针的指针。但是,我的IDE抱怨指针类型不兼容。我无法找到任何签名和历史访问器的组合,这些组合允许我返回任何有用的内容

这在C语言中是可能的吗

warning: returning 'history_t (*)[6]' {aka 'struct <anonymous> (*)[6]'} from a function with incompatible return type 'history_t **' {aka 'struct <anonymous> **'} [-Wincompatible-pointer-types]
     return history;
警告:从具有不兼容返回类型“history\u t**”{aka'struct**}[-Wincompatible指针类型]的函数返回“history\u t(*)[6]{aka'struct(*)[6]}
回归历史;

因此,我的函数不是返回历史**,而是返回历史*[6]。但是,重新定义签名以返回
历史*[6]
也不起作用。

您有一个静态数组,在您的情况下,该数组必须具有固定大小的
6
。如果你必须返回一个6的指针

history\u t(*get\u history())[6]{
回归历史;
}
然后调用这个函数并使用它的结果,您可以

history_t(*标识符)[6]=get_history();

请参阅关于指针、静态和其他…

关于数组、多维数组和指针有许多误解。我将尝试解释它们之间的区别,然后回答问题。下面,
T
表示类型名称

T x[3]; /* the type of x is "array of T" */
x
是一个数组。它有三个要素。每个元素都是一个
T

T *x[3]; /* the type of x is "array of pointer to T" */
x
是一个数组。它有三个要素。每个元素都是指向T的
指针

T (*x)[3]; /* the type of x is "pointer to an array of three Ts" */
x
是一个指针。它指向一个数组。数组有三个元素。每个元素都是一个
T

T x[3][6]; /* the type of x is "array of array of six Ts" */
x
是一个数组。它有三个要素。每个元素是六个
T
s的数组。因此,
x
是一个多维数组,或者更准确地说,是一个二维数组,一个3×6的Ts数组

现在,C中有一条非常重要的规则:类型为“array of T”的表达式被转换为指向数组对象初始元素的“指向T的指针”,除非它是
sizeof
或address操作符的操作数。所以

void f(T*);

void g(void)
{
    T x[3];
    /* ... */
    f(x); /* equivalent to f(&x[0]); */
}
指向数组
x
的第一个元素的指针在上面的函数调用
f(x)
中作为参数传递。数组未通过。在C语言中,不能将数组作为参数直接传递给函数(可以通过将数组包装到结构中间接实现)。从函数返回具有相同的语义:数组不能从函数返回(除非它包装在结构中):

返回指向数组
x
初始元素的指针

您的数组定义为

static history_t history[3][6]; /* static is not relevant in this discussion */ 
history_t (*x)[6];
history
的类型是“六个
history\u t
s数组”。如上所述,当从函数返回时,它将被转换为“指向六个
history\u t
s数组的指针”。类型为“指向六个
history\u t
s的数组指针”的对象
x
,定义为

static history_t history[3][6]; /* static is not relevant in this discussion */ 
history_t (*x)[6];
要声明返回该类型的函数,函数声明符将替换
x

history_t (*get_history())[6]; /* function returning pointer to array of six history_ts */
()
的优先级高于
*
,因此
get_history()
周围不需要括号。为了定义这样一个函数,增加了函数体:

static history_t history[3][6];

history_t (*get_history())[6]
{
    /* ... */
    return history;
}

“函数声明器后的预期函数体”是我尝试此操作时得到的警告。
history\u t(*get\u history())[6]{return history;}
您能解释一下这里发生了什么吗?这会传递一个副本或对2d数组的引用吗?您只需像调用任何不带参数的函数一样调用它。@DanielPaczuskiBak get_history是一个返回指向历史数组[6]指针的函数。它不返回副本。它返回指向历史记录的6元素数组的指针。数组的数组不是指向指针的指针