我想构造一个包含未知行数和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};
代码编译失败。如何解决问题?
答案 0 :(得分:2)
除了拼写错误cinst
和丢失的分号和假设(不让我们知道)s
和struct s
属于同一类型
您的问题是str1
和结构成员s
的类型不兼容
str1
的类型为const char*[][2]
const char***
强制编译器假定str1
类型const char ***
解决了您的直接问题,即程序编译并“正常工作”,但您确实需要了解数组不是指针和指针不是数组。请参阅c-faq site的第6部分。
struct s
{
const char*** s;
};
const char* str1[][2] = {"1","2",
"3","4",
"5","6"};
struct s s1 = {(const char ***)str1};
const char* str2[][2] = {"1","2",
"3","4"};
struct s s2 = {(const char ***)str2};
答案 1 :(得分:0)
嗯,它不是C.(并且缺少分号)
struct s {...};不引入类型“s”,不是typedef。 例如你不能使用: struct s {...}; s my_thing; (即使你添加了缺少的分号) 也许你在混淆C和C ++?
答案 2 :(得分:-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};
删除第三个间接/解除引用将解决此问题。