C 硬编码 typedef 结构数组

发布于 2024-07-17 18:16:42 字数 688 浏览 5 评论 0原文

这是一个非常愚蠢的问题,即使问出来也令人沮丧。 请耐心听我说,今天我对这个感到特别愚蠢......

我有一个具有某种 typedef 结构的库。 基本上:

typedef struct {int x; int y;} coords;

我真正想做的是在我的标头中声明一个这样的变量数组:(

coords MyCoords[];

我最近将其切换为 coords *MyCoords; 然后运行 ​​MyCoords = new coords[4 ];)

,然后在我的 init 中我想对其中的一些值进行硬编码。

MyCoords[] = {{0, 0}, {2, 2} etc. 

我今天一定是在胡思乱想,因为它不允许我以任何方式简单地输入一些简单的东西,比如 MyCoords[0] = {0, 0}

对上面的一些语法感到抱歉。 谢谢你的帮助。 我什至不关心一次对所有值进行硬编码。 我现在只要让 MyCoords[0] = {0, 0} 工作就可以了。 我可以这样做 MyCoords[0].x = 0; MyCoords[0].y = 0 但我永远不会弄清楚我做错了什么。 非常感谢任何人..

This is such a dumb question it's frustrating even asking it. Please, bear with me, I'm feeling particularly dumb over this one today....

I've got a library with a certain typedef struct. basically:

typedef struct {int x; int y;} coords;

What I really wanted to do was declare in my header a variable array of this:

coords MyCoords[];

(I recently switched it to just coords *MyCoords; and then ran a MyCoords = new coords[4];)

and then in my init I wanted to hardcode some values in it.

MyCoords[] = {{0, 0}, {2, 2} etc. 

I must just be brainfarting today because it's not letting me do any manner of simply putting in even something simple like MyCoords[0] = {0, 0}

sorry about some of the syntax above. Thanks for any help. I don't even care about hard-coding all the values at once. I'm now fine with just geting MyCoords[0] = {0, 0} working. I could do MyCoords[0].x = 0; MyCoords[0].y = 0 but then I'd never figure out what I'm doing wrong. Thanks a lot anyone..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

长梦不多时 2024-07-24 18:16:42

您只能在声明数组的同一位置初始化数组。 可行的形式是:

coords MyCoords[] = {{0, 0}, {2, 2}};

如果您需要在标头中声明 MyCoords 以便许多模块可以访问它,我认为以下方法可行:

extern coords MyCoords[];

You can only initialize an array at the same spot you declare it. The form that will work is:

coords MyCoords[] = {{0, 0}, {2, 2}};

If you need to declare MyCoords in a header so that many modules can access it, I think the following will work:

extern coords MyCoords[];
枯寂 2024-07-24 18:16:42

好吧,您创建的不是变量数组。 这是一个大小未知的数组。 该数组的类型不完整,因此无法在 C++ 中定义。 您可以做的就是在其前面添加 extern 使其只是一个声明。

extern coords MyCoords[];

然后,在初始化它的 .cpp 文件中,您可以创建(定义)该数组 - 当然,请注意您应该在命名空间范围内、在任何函数之外执行此操作:

coords MyCoords[] = {{1, 2}, {3, 4}, ... };

如果您想要的是一个其大小未知的数组编译时,而是在运行时,您应该使用 std::vector :

std::vector< coords > MyCoords;

然后如果您填充它,请执行

MyCoords.push_back( coords(1, 2) );
MyCoords.push_back( coords(3, 4) );
....

或创建一个本地数组,然后使用元素的副本初始化向量该数组:

coords c[] = ....;
MyCoords.insert(MyCoords.end(), c, c + sizeof c / sizeof *c);

嗯,在 C 中,它与 C++ 中略有不同。 您可以定义一个维度未知的数组。 所发生的情况是,它被视为“暂定”定义。 如果在文件的末尾(更准确地说,它的翻译单元,即该文件以及它包含的所有文件 - 意味着编译器一起翻译的所有文件)没有包含大小的后续定义,则大小被取为1。 但是,在 C++ 中,没有暂定定义,并且不包含大小且不包含可以判断大小的初始值设定项的定义是无效的 C++ 程序。

由于您的代码变得无效,您正在做的

MyCoords[0] = {0, 0}

事情编译器将读取为:

-> Set LHS to RHS
-> -> LHS is a variable of type `coords`. Fine
-> -> RHS is... hmm, `{0, 0}`. What the heck is it??

您看,编译器不知道右侧应该意味着什么。 为此,C99(1999 版 C)引入了所谓的复合文字。 它允许你编写

MyCoords[0] = (coords){0, 0};

它实际上会做你想做的事情 - 创建一个 coord 类型的右侧值并将其分配给左侧。 但是出于兼容性原因(许多 C 编译器不合理地兼容 C99,并且 C++ 和 C89 都不支持复合文字),您应该使用我之前向您展示的方法之一。

Well, what you created is not a variable array. It's an array whose size is not known. That array has an incomplete type, and can thus not be defined in C++. What you can do is to make it just a declaration by putting extern before it.

extern coords MyCoords[];

Then, in the .cpp file that initializes it, you can then create (define) that array - beware you should do it at namespace scope, outside any function, of course:

coords MyCoords[] = {{1, 2}, {3, 4}, ... };

If what you want is an array whose size is not known at compile time yet, but rather at runtime, you should use a std::vector:

std::vector< coords > MyCoords;

Then later if you fill it, do

MyCoords.push_back( coords(1, 2) );
MyCoords.push_back( coords(3, 4) );
....

Or create a local array and then initialize the vector using a copy of the elements of that array:

coords c[] = ....;
MyCoords.insert(MyCoords.end(), c, c + sizeof c / sizeof *c);

Well, in C, it is slightly different than in C++. You can define an array whose dimension isn't known. What happens is that it's taken to be a "tentative" definition. If at the end of the file (more precisely, of its translation unit, which is that file, and all the files it includes - meaning everything the compiler translates together) there is not a subsequent definition of it that includes the size, then the size is taken to be 1. However, in C++ there are no tentative definitions, and a definition that does not include the size and does not contain an initializer that can tell the size is an invalid C++ program.

For the reason why your code goes grazy, you are doing

MyCoords[0] = {0, 0}

Which the compiler will read as:

-> Set LHS to RHS
-> -> LHS is a variable of type `coords`. Fine
-> -> RHS is... hmm, `{0, 0}`. What the heck is it??

You see, the compiler has no clue what the right hand side is supposed to mean. C99, for that reason (1999 version of C) introduced so-called compound literals. It allows you to write

MyCoords[0] = (coords){0, 0};

And it will actually do what you want it to do - creating a right hand side value of type coord and assigns it to the left hand side. But you should just, already from a compatibility reason (many C compilers are not reasonable C99 compliant - and neither C++ nor C89 supports compound literals), use one of the previous ways i showed you.

谁的新欢旧爱 2024-07-24 18:16:42

我不确定这在您的实际代码中是否也是一个问题,但是:

typedef struct coords {int x, int y};

不是 C 中完整的 typedef 声明,因为它没有为 type 提供名称,仅为结构提供名称(结构标记与标准 C 中的类型名称不在同一命名空间中)。 为了使 typedef 声明正确,您必须为整个类型 struct coords {int x, int y} 指定一个名称,就像声明该类型的变量一样,如下所示:

typedef struct coords {int x, int y} coords;

I'm not sure whether this is also a problem in your actual code, but:

typedef struct coords {int x, int y};

is not a complete typedef declaration in C, as it does not give a name to the type, only to the struct (struct tags are not in the same namespace as type names in standard C). To make the typedef declaration correct, you have to specify a name for the whole type struct coords {int x, int y}, as if you were declaring a variable of that type, like so:

typedef struct coords {int x, int y} coords;
凡尘雨 2024-07-24 18:16:42

它应该是:

typedef struct coords {int x, int y} coords;
coords MyCoords[] = {{0, 0}, {2, 2}, {3, 4}};

It should be:

typedef struct coords {int x, int y} coords;
coords MyCoords[] = {{0, 0}, {2, 2}, {3, 4}};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文