Home > Back-end >  How to use a pointer to use the members of a structure inside a nested structure?
How to use a pointer to use the members of a structure inside a nested structure?

Time:12-02

Consider the following piece of code:

typedef struct
{
    int out;
    struct
    {
        int in1;
        struct
        {
            int in_int2;
            char in_char2;
        } inner2[3];
    } inner1[2];
} outer;

outer o1;

How do I use a pointer to point to inner structures, say o1.inner[0].inner[1] and assign the values to the same?

i.e. (pseudo code provided for explanation. Exact syntax is being asked:

pointer *my_p = o1.inner[0].inner[1];
my_p->in_int2 = 2;

CodePudding user response:

All of your inner structures are anonymous, so there's no way to reference them. You need to give each struct a name:

typedef struct
{
int out ;
   struct inner1
   {
      int in1;
      struct inner2
      {
          int in_int2;
          char in_char2;
      }inner2[3];
   }inner1[2];
}outer;

Then you can create a pointer to the correct type:

struct inner2 *my_p = &o1.inner1[0].inner2[1];

CodePudding user response:

The only options in pure c you would have are:

1. Give the inner structs a name

That way you can reference the concrete type of the inner struct to declare the pointer:

typedef struct
{
    int out;
    struct inner1
    {
        int in1;
        struct inner2
        {
            int in_int2;
            char in_char2;
        } inner2[3];
    } inner1[2];
} outer;

// Usage:
int main() {
    outer o1;
    struct inner2 *ptr = &o1.inner1[1].inner2[2];
    // ...
}

2. Define a second equivalent struct as the pointer type

By defining a struct with the exact same members you can cast the pointer to that known struct.
This might be an option in case you can't modify the definition of outer.

typedef struct
{
    int out;
    struct
    {
        int in1;
        struct
        {
            int in_int2;
            char in_char2;
        } inner2[3];
    } inner1[2];
} outer;

// Usage:
int main() {
    outer o1;

    struct inner_type { int in_int2; char in_char2; };
    struct inner_type* ptr = (struct inner_type*)&o1.inner1[1].inner2[2];

    // ...
}

3. Use pointers directly to the named members

You could also just use pointers to in_int2 / in_char2, that way you don't have to deal with the unnamed structs:

typedef struct
{
    int out;
    struct
    {
        int in1;
        struct
        {
            int in_int2;
            char in_char2;
        } inner2[3];
    } inner1[2];
} outer;

int main() {
    outer o1;

    int* ptr_in_int2 = &o1.inner1[1].inner2[2].in_int2;
    char* ptr_in_char2 =  &o1.inner1[1].inner2[2].in_char2;

    // ...
}

4. non-standard typeof()

If you can use compiler extensions, both gcc and clang provide typeof(), which allows you to get the type of an arbitrary expression.

You can use this to create a pointer to the unnamed struct, e.g.:

typedef struct
{
    int out;
    struct
    {
        int in1;
        struct
        {
            int in_int2;
            char in_char2;
        } inner2[3];
    } inner1[2];
} outer;

int main() {
    outer o1;
    typeof(o1.inner1[0].inner2[0])* ptr = &o1.inner1[1].inner2[2];
    // ...
}
  • Related