C“通用”结构构建
我想构造一个结构体,其中有一个未知行数和 2 列数的数组 类似
struct s
{
cinst char*** s;
}
const char* str1[][2] = {"1","2",
"3","4",
"5","6"};
s s1 = {str1};
const char* str2[][2] = {"1","2",
"3","4"};
s s2 = {str2};
代码编译失败。我该如何解决这个问题?
I want to construct a struct which has an array of unknown number of rows and 2 columns
something like
struct s
{
cinst char*** s;
}
const char* str1[][2] = {"1","2",
"3","4",
"5","6"};
s s1 = {str1};
const char* str2[][2] = {"1","2",
"3","4"};
s s2 = {str2};
The code fails in compilation.How can I solve the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了拼写错误
cinst
和缺少分号以及假设(不让我们知道)s
和struct s
是同一类型之外,您的问题是
str1
的类型和结构成员s
不兼容str1
的类型为const char*[][2]
const char***
强制编译器假设
str1
的类型为const char ***
解决了您眼前的问题,即程序编译和“有效”,但您确实需要了解数组不是指针并且指针不是数组。请参阅 c-faq 网站 的第 6 部分。Aside from the typo
cinst
and the missing semicolon and assuming (without letting us know)s
andstruct s
are the same typeyour problem is that the types of
str1
and the struct members
are not compatiblestr1
is of typeconst char*[][2]
const char***
Forcing the compiler to assume
str1
is of typeconst char ***
solves your immediate problem, ie, the program compiles and "works", but you really need to understand that arrays are not pointers and pointers are not arrays. See section 6 of the c-faq site.好吧,它不是 C。(并且缺少一个分号)
struct s { ...};没有引入类型“s”,它不是 typedef。
例如你不能使用:
结构 s { ...};
是我的事;
(即使您添加缺少的分号)
也许您混淆了 C 和 C++?
Well, it is not C. (and there is a semicolon missing)
struct s { ...}; does not introduce a type "s", it is not a typedef.
eg you cannot use:
struct s { ...};
s my_thing;
(even if you add the missing semicolon)
Maybe you are confusing C and C++ ?
删除第三个间接/取消引用将解决此问题。
Removing the third indirection / dereference will fix this.