C 和动态结构元素访问

发布于 2024-10-28 20:22:40 字数 893 浏览 2 评论 0原文

我有这个复杂的结构:

#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 技术交流群。

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

发布评论

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

评论(6

滥情稳全场 2024-11-04 20:22:40

你的问题并不在你说的地方。您发布的代码给出了编译错误:

error: request for member ‘z’ in something not a structure or union

在该行,

example.z = malloc(sizeof(STRUCT));

因为您打算编写 example->z,因为 example 是指向 STRUCT 的指针>,而不是 STRUCT

从那里开始,您可以完全按照您所说的那样访问 example->z[i].x 。这种语法一直很好。

例如:

/* your declarations here */

example = malloc(sizeof(STRUCT));
example->z = malloc(NUM * sizeof(SUB));

for(i = 0; i < NUM; ++i) {
    example->z[i].x = i;
    example->z[i].y = -i;
    printf("%d %d\n", example->z[i].x, example->z[i].y);
}

/* output:
0 0
1 -1
2 -2
3 -3
4 -4
*/

Your problem isn't where you say it is. Your code as posted gives a compile error:

error: request for member ‘z’ in something not a structure or union

at the line

example.z = malloc(sizeof(STRUCT));

because you meant to write example->z, since example is a pointer to STRUCT, not a STRUCT.

From there on, you can access example->z[i].x exactly as you said. That syntax has always been fine.

For example:

/* your declarations here */

example = malloc(sizeof(STRUCT));
example->z = malloc(NUM * sizeof(SUB));

for(i = 0; i < NUM; ++i) {
    example->z[i].x = i;
    example->z[i].y = -i;
    printf("%d %d\n", example->z[i].x, example->z[i].y);
}

/* output:
0 0
1 -1
2 -2
3 -3
4 -4
*/
白首有我共你 2024-11-04 20:22:40

当您有指向指针的指针时,您经常会遇到优先级问题。我不记得这是否是一个,但您可以尝试 (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.

一直在等你来 2024-11-04 20:22:40

首先,你的第二个malloc是错误的; example 是一个指针,所以 this:

example.z = malloc(NUM * sizeof(SUB));

应该是这样:

example->z = malloc(NUM * sizeof(SUB));

然后在你的循环中你可以这样说:

example->z[i].x = i;
example->z[i].y = i;

你还希望将它放在文件顶部附近:

#include <stdlib.h>

First of all, your second malloc is wrong; example is a pointer so this:

example.z = malloc(NUM * sizeof(SUB));

should be this:

example->z = malloc(NUM * sizeof(SUB));

Then in your loop you can say things like this:

example->z[i].x = i;
example->z[i].y = i;

You'll also want to have this near the top of your file:

#include <stdlib.h>
分分钟 2024-11-04 20:22:40

试试这个:

int my_x = example[3].z[2].x;
  • 上面的代码将首先访问 example[3] (示例数组的第四个元素)。
  • 一旦获得该特定元素,就可以像访问普通对象一样自动访问其内容。
  • 然后您可以从该元素访问 z[2]。请注意,example[3] 是一个元素,因此您可以使用 . 来访问其成员;如果它是一个数组,您可以将其作为数组访问。
  • 所以到目前为止,example[3].z[2]example 的 one 元素内 SUB 数组的 one 元素数组。
  • 现在您可以使用上面所示的方式简单地访问成员x

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

typedef struct {
    int a;
    SUB *z;
} STRUCT;

STRUCT *example;

int main() {
    example = malloc(sizeof(STRUCT)*10); //array of 10;
    int i=0,j=0;
    for (;i<10;i++){
        example[i].a = i;
        example[i].z = malloc(sizeof(SUB)*5);
        for (j=0; j<5; j++)
            example[i].z[j].x = example[i].z[j].y = j;
    }
    //access example[3] and access z[2] inside it. And finally access 'x'
    int my_x = example[3].z[2].x;
    printf("%d",my_x);

    for (i=0;i<10;i++){
        printf("%d |\n",example[i].a);
        //example[i].z = malloc(sizeof(SUB)*5);
        for (j=0; j<5; j++)
            printf("%d %d\n",example[i].z[j].x,example[i].z[j].y);
        free(example[i].z);
    }
    free(example);
}

Try this:

int my_x = example[3].z[2].x;
  • The above code will first access the example[3] (the fourth element of the example array).
  • Once you get that particular element, its contents can be automatically access in the same way as you do with normal objects.
  • You then access z[2] from that element. Note that, 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.
  • So till now, example[3].z[2] is one element of the SUB array inside one element of the example array.
  • Now you can simply access the member x using the way shown above.

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

typedef struct {
    int a;
    SUB *z;
} STRUCT;

STRUCT *example;

int main() {
    example = malloc(sizeof(STRUCT)*10); //array of 10;
    int i=0,j=0;
    for (;i<10;i++){
        example[i].a = i;
        example[i].z = malloc(sizeof(SUB)*5);
        for (j=0; j<5; j++)
            example[i].z[j].x = example[i].z[j].y = j;
    }
    //access example[3] and access z[2] inside it. And finally access 'x'
    int my_x = example[3].z[2].x;
    printf("%d",my_x);

    for (i=0;i<10;i++){
        printf("%d |\n",example[i].a);
        //example[i].z = malloc(sizeof(SUB)*5);
        for (j=0; j<5; j++)
            printf("%d %d\n",example[i].z[j].x,example[i].z[j].y);
        free(example[i].z);
    }
    free(example);
}

哎呦我呸! 2024-11-04 20:22:40

在“破旧的解决方法”中,您写道:

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

这里的问题是 C 按指向的对象的大小缩放指针,因此当您向 SUB 指针添加 1 时,该值会提前 <代码>sizeof(SUB)。所以,你只需要:

SUB *ptr = example->z;
int i;

for (i = 0; i < NUM; ++i) {
    ptr->x = ptr->y = 0;
    ptr++;
}

当然,正如其他人所说,你也可以这样做(假设是C99):

for (int i = 0; i < NUM; ++i)
    example->z[i].x = example->z[i].y = 0;

In the 'shabby workaround', you wrote:

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

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 by sizeof(SUB). So, you simply need:

SUB *ptr = example->z;
int i;

for (i = 0; i < NUM; ++i) {
    ptr->x = ptr->y = 0;
    ptr++;
}

Of course, as others have said, you can also do (assuming C99):

for (int i = 0; i < NUM; ++i)
    example->z[i].x = example->z[i].y = 0;
宣告ˉ结束 2024-11-04 20:22:40
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

#define NUM 5

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

typedef struct
{
    int a;
    SUB* z;
}STRUCT;

void main(void)
{
    clrscr();
    printf("Sample problem..\n\n");

    STRUCT* example;
    int i;

    example = (STRUCT*)malloc(sizeof(STRUCT));
    example->z = (SUB*)malloc(NUM * sizeof(SUB));

    for(i = 0; i < NUM; i++)
    {
        example->z[i].x = i +1;
        example->z[i].y = (example->z[i].x)+1;
        printf("i = %d: x:%d y:%d\n", i, example->z[i].x, example->z[i].y);
    }
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

#define NUM 5

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

typedef struct
{
    int a;
    SUB* z;
}STRUCT;

void main(void)
{
    clrscr();
    printf("Sample problem..\n\n");

    STRUCT* example;
    int i;

    example = (STRUCT*)malloc(sizeof(STRUCT));
    example->z = (SUB*)malloc(NUM * sizeof(SUB));

    for(i = 0; i < NUM; i++)
    {
        example->z[i].x = i +1;
        example->z[i].y = (example->z[i].x)+1;
        printf("i = %d: x:%d y:%d\n", i, example->z[i].x, example->z[i].y);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文