C“通用”结构构建

发布于 2024-12-01 14:32:37 字数 326 浏览 0 评论 0原文

我想构造一个结构体,其中有一个未知行数和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以为你会在 2024-12-08 14:32:37

除了拼写错误 cinst 和缺少分号以及假设(不让我们知道) sstruct s 是同一类型之外,

您的问题是str1 的类型和结构成员 s 不兼容

  • str1 的类型为 const char*[][2]
  • 结构成员的类型const char***

强制编译器假设 str1 的类型为 const char *** 解决了您眼前的问题,即程序编译和“有效”,但您确实需要了解数组不是指针并且指针不是数组。请参阅 c-faq 网站 的第 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};

Aside from the typo cinst and the missing semicolon and assuming (without letting us know) s and struct s are the same type

your problem is that the types of str1 and the struct member s are not compatible

  • str1 is of type const char*[][2]
  • the struct member is of type const char***

Forcing the compiler to assume str1 is of type const 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.

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};
柠檬色的秋千 2024-12-08 14:32:37

好吧,它不是 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++ ?

橘虞初梦 2024-12-08 14:32:37
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};

删除第三个间接/取消引用将解决此问题。

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};

Removing the third indirection / dereference will fix this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文