daemoner119 daemoner119 - 3 months ago 7
C++ Question

Accessing elements within a pointer of a struct inside another pointer to a struct

Just trying to learn how nested pointers to structs work. How would I access an element within a pointer of a struct within another pointer to a struct? Thanks.

#include <iostream>

typedef struct
{
unsigned int a;
unsigned int b;
unsigned int c;
} inner;

typedef struct
{
unsigned int d;
unsigned int e;
inner * in;
} outer;

/* Function to use struct */
void test(outer * out)
{
//Update the value of out->in->a.
out->in->a = 5;
}

int main() {
outer * out;
test(out);

std::cout << "My updated value: " << out->in->a << std::endl;
}


I get a "Segmentation fault (core dumped)".

Edit: Made changes based on suggestions.

#include <iostream>

typedef struct
{
unsigned int a;
unsigned int b;
unsigned int c;
} inner;

typedef struct
{
unsigned int d;
unsigned int e;
inner * in = new inner();
} outer;

/* Function to use struct */
void test(outer * out)
{
//Update the value of out->d.
out->d = 3;
//Update the value of out->in->a.
out->in->a = 5;
}

int main() {

outer out;
outer* p = &out;

test(p);

std::cout << "My updated value for d: " << p->d << std::endl;
std::cout << "My updated value for a: " << p->in->a << std::endl;
}


I now get the correct values updated. However I have a warning now

"warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11"

inner * in = new inner();


How do I fix this warning?

Answer

For starters C and C++ are different languages.

In any case you have to allocate the structures before accessing their elements throw pointers. Otherwise using uninitialized pointers or pointers that do not point to valid structures' objects results in undefined behavior of the program.

Here is a demonstrative C program that shows how to access elements of a structure using pointers

#include <stdlib.h>
#include <stdio.h>

typedef struct
{
  unsigned int            a;
  unsigned int            b;
  unsigned int            c;
} inner;

typedef struct
{
  unsigned int            d;
  unsigned int            e;
  inner * in;
} outer;


int main( void ) 
{
    outer *out = malloc( sizeof( outer ) );

    if ( ( out != NULL ) && ( out->in = malloc( sizeof( inner ) ) ) )
    {
        out->in->a = 1;
        out->in->b = 2;
        out->in->c = 3;

        printf( "out->in = { a = %d, b = %d, c = %d }\n", out->in->a, out->in->b, out->in->c );

        free( out->in );
        free( out );
    }

    return 0;
}

Its output is

out->in = { a = 1, b = 2, c = 3 }

In C++ the program can look like

#include <iostream>

typedef struct
{
  unsigned int            a;
  unsigned int            b;
  unsigned int            c;
} inner;

typedef struct
{
  unsigned int            d;
  unsigned int            e;
  inner * in;
} outer;

int main() 
{
    outer *out = new outer;

    out->in = new inner;

    out->in->a = 1;
    out->in->b = 2;
    out->in->c = 3;

    std::cout << "out->in = { a = " << out->in->a
              << ", b = " << out->in->b
              << ", c = " << out->in->c
              << " }" << std::endl;

    delete out->in;
    delete out;

    return 0;
}

Its output will be the same as shown above.

As for this function

void test(outer *out)
{
  //Update the value of out->in->a.
  out->in->a = 5;
}

then the both pointers out and in must point to valid objects of types outer and inner.

Comments