开发者

C and dynamic structure element access

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开发者_JAVA技巧 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);
}


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
*/


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.


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>


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


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;


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜