C 和动态结构元素访问
我有这个复杂的结构:
#include <stdlib.h>
typedef struct {
int x;
int y;
} SUB;
typedef struct {
int a;
SUB *z;
} STRUCT;
#define NUM 5
int main(void)
{
STRUCT *example;
int i;
example = malloc(sizeof(STRUCT));
example->z = malloc(NUM * sizeof(SUB));
for(i = 0; i < NUM; ++i) {
/* how do I access variable in certain struct of array of z's */
}
return 0;
}
example
是动态分配的结构,example
内的z
是动态分配的SUB 结构。
如何访问结构 z
的特定元素中的特定变量?
我一直在尝试这样的事情: example->z[i].x
但它似乎不起作用。
目前我正在使用这个看起来破旧的解决方法:
SUB *ptr = example->z;
int i;
for(i = 0; i < amount_of_z_structs; ++i) {
/* do something with 'ptr->x' and 'ptr->y' */
ptr += sizeof(SUB);
}
I have this complicated structure thingie:
#include <stdlib.h>
typedef struct {
int x;
int y;
} SUB;
typedef struct {
int a;
SUB *z;
} STRUCT;
#define NUM 5
int main(void)
{
STRUCT *example;
int i;
example = malloc(sizeof(STRUCT));
example->z = malloc(NUM * sizeof(SUB));
for(i = 0; i < NUM; ++i) {
/* how do I access variable in certain struct of array of z's */
}
return 0;
}
example
is dynamically allocated structure and z
inside the example
is dynamically allocated array of SUB
structures.
How do I access certain variable in certain element of structure z
?
I have been trying something like this: example->z[i].x
but it doesnt seem to work.
At the moment I am using this shabby looking workaraound:
SUB *ptr = example->z;
int i;
for(i = 0; i < amount_of_z_structs; ++i) {
/* do something with 'ptr->x' and 'ptr->y' */
ptr += sizeof(SUB);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的问题并不在你说的地方。您发布的代码给出了编译错误:
在该行,
因为您打算编写
example->z
,因为example
是指向STRUCT
的指针>,而不是STRUCT
。从那里开始,您可以完全按照您所说的那样访问
example->z[i].x
。这种语法一直很好。例如:
Your problem isn't where you say it is. Your code as posted gives a compile error:
at the line
because you meant to write
example->z
, sinceexample
is a pointer toSTRUCT
, not aSTRUCT
.From there on, you can access
example->z[i].x
exactly as you said. That syntax has always been fine.For example:
当您有指向指针的指针时,您经常会遇到优先级问题。我不记得这是否是一个,但您可以尝试
(example->b)[i].x
。When you have pointers pointing to pointers you often end up running into precedence issues. I can't recall if this is one, but you might try
(example->b)[i].x
.首先,你的第二个
malloc
是错误的;example
是一个指针,所以 this:应该是这样:
然后在你的循环中你可以这样说:
你还希望将它放在文件顶部附近:
First of all, your second
malloc
is wrong;example
is a pointer so this:should be this:
Then in your loop you can say things like this:
You'll also want to have this near the top of your file:
试试这个:
example[3]
是一个元素,因此您可以使用.
来访问其成员;如果它是一个数组,您可以将其作为数组访问。example[3].z[2]
是example 的 one 元素内 SUB 数组的 one 元素数组。
x
。Try this:
example[3]
is an element, so you could use a.
to access its members; if its an array, you can access it as an array.example[3].z[2]
is one element of the SUB array inside one element of theexample
array.x
using the way shown above.在“破旧的解决方法”中,您写道:
这里的问题是 C 按指向的对象的大小缩放指针,因此当您向
SUB
指针添加 1 时,该值会提前 <代码>sizeof(SUB)。所以,你只需要:当然,正如其他人所说,你也可以这样做(假设是C99):
In the 'shabby workaround', you wrote:
The problem here is that C scales pointers by the size of the object pointed to, so when you add 1 to a
SUB
pointer, the value is advanced bysizeof(SUB)
. So, you simply need:Of course, as others have said, you can also do (assuming C99):