C 硬编码 typedef 结构数组
这是一个非常愚蠢的问题,即使问出来也令人沮丧。 请耐心听我说,今天我对这个感到特别愚蠢......
我有一个具有某种 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只能在声明数组的同一位置初始化数组。 可行的形式是:
如果您需要在标头中声明 MyCoords 以便许多模块可以访问它,我认为以下方法可行:
You can only initialize an array at the same spot you declare it. The form that will work is:
If you need to declare MyCoords in a header so that many modules can access it, I think the following will work:
好吧,您创建的不是变量数组。 这是一个大小未知的数组。 该数组的类型不完整,因此无法在 C++ 中定义。 您可以做的就是在其前面添加
extern
使其只是一个声明。然后,在初始化它的 .cpp 文件中,您可以创建(定义)该数组 - 当然,请注意您应该在命名空间范围内、在任何函数之外执行此操作:
如果您想要的是一个其大小未知的数组编译时,而是在运行时,您应该使用 std::vector :
然后如果您填充它,请执行
或创建一个本地数组,然后使用元素的副本初始化向量该数组:
嗯,在 C 中,它与 C++ 中略有不同。 您可以定义一个维度未知的数组。 所发生的情况是,它被视为“暂定”定义。 如果在文件的末尾(更准确地说,它的翻译单元,即该文件以及它包含的所有文件 - 意味着编译器一起翻译的所有文件)没有包含大小的后续定义,则大小被取为
1
。 但是,在 C++ 中,没有暂定定义,并且不包含大小且不包含可以判断大小的初始值设定项的定义是无效的 C++ 程序。由于您的代码变得无效,您正在做的
事情编译器将读取为:
您看,编译器不知道右侧应该意味着什么。 为此,C99(1999 版 C)引入了所谓的复合文字。 它允许你编写
它实际上会做你想做的事情 - 创建一个
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.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:
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
:Then later if you fill it, do
Or create a local array and then initialize the vector using a copy of the elements of that array:
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
Which the compiler will read as:
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
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.我不确定这在您的实际代码中是否也是一个问题,但是:
不是 C 中完整的 typedef 声明,因为它没有为 type 提供名称,仅为结构提供名称(结构标记与标准 C 中的类型名称不在同一命名空间中)。 为了使 typedef 声明正确,您必须为整个类型 struct coords {int x, int y} 指定一个名称,就像声明该类型的变量一样,如下所示:
I'm not sure whether this is also a problem in your actual code, but:
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:它应该是:
It should be: