[英]assigning 2D array as a struct member
I need to create a struct with a 2D bool array as member, so I made it double pointer as shown below.我需要创建一个以 2D bool 数组作为成员的结构,因此我将其设为双指针,如下所示。 No I have a problem whenever I try to assign 2D array object to this struct member, I receive a warning that It's incompitable pointer type.
不,每当我尝试将二维数组 object 分配给这个结构成员时,我都会遇到问题,我会收到一条警告,指出它是不可兼容的指针类型。 Is there anyway to assign it (Not copy because I don't have an object only double pointer as a struct member)
无论如何要分配它(不复制,因为我没有 object 只有双指针作为结构成员)
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct
{
bool** object;
}entry_t;
bool testObject[3][6];
entry_t entry =
{
.object = testObject
};
The warning received收到的警告
warning: initialization of '_Bool **' from incompatible pointer type '_Bool (*)[6]' [-Wincompatible-pointer-types]
A pointer-to-pointer is not an array.指针指向指针不是数组。 It is not a 2D array.
它不是二维数组。 It is not a pointer to an array.
它不是指向数组的指针。 It can't be made to point at arrays.
它不能指向 arrays。
A very special use-case of pointer-to-pointer is to have it point at the first member of an array of pointers.指针对指针的一个非常特殊的用例是让它指向指针数组的第一个成员。 This is mostly just useful when declaring an array of pointers to strings.
这在声明指向字符串的指针数组时非常有用。
bool testObject[3][6];
is a 2D array - an array of arrays.是一个二维数组 - arrays 的数组。 The first item is an array of type
bool [6]
.第一项是
bool [6]
类型的数组。 In order to point at it, you need a pointer of type bool (*)[6]
.为了指向它,您需要一个
bool (*)[6]
类型的指针。 The correct code is therefore:因此正确的代码是:
typedef struct
{
bool (*object)[6];
}entry_t;
bool testObject[3][6];
entry_t entry =
{
.object = testObject
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.