Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize struct contain references to structs

Is it possible to have a struct containing references to structs. And how are these initialized? See short example below.

Thanks

typedef struct {
  int a;
}typeInner1;


typedef struct {
  int b;
}typeInner2;


typedef struct {
  typeInner1 &one;
  typeInner2 &two;
}typeOuter;

void fun2(typeOuter *p){
  p->one.a =2;
  p->two.b =3;
}


void fun(typeInner1 &arg1,typeInner2 &arg2){
  typeOuter *ptr = new typeOuter;//<-- how to write initializer
  fun2(ptr);
}


int main(){
  typeInner1 arg1;
  typeInner2 arg2;
  fun(arg1,arg2);

  //now arg1.a should be 2 and arg2.a=3
}

Ok thanks for all the input. I also had to modify the typedef of the typeOuter to make it work. Full working code below for other people finding this post.

#include <cstdio>
typedef struct {
  int a;
}typeInner1;


typedef struct {
  int b;
}typeInner2;


typedef struct typeOuter_t {
  typeInner1 &one;
  typeInner2 &two;
  typeOuter_t(typeInner1 &a1, typeInner2 &a2) : one(a1), two(a2) {}
}typeOuter;

void fun2(typeOuter *p){
  p->one.a =2;
  p->two.b =3;
}


void fun(typeInner1 &arg1,typeInner2 &arg2){
  typeOuter *ptr = new typeOuter(arg1,arg2);
  fun2(ptr);
}


int main(){
  typeInner1 arg1;
  typeInner2 arg2;
  fun(arg1,arg2);

  //now arg1.a shoule be 1 and arg2.a=3
  fprintf(stderr,"arg1=%d arg2=%d\n",arg1.a,arg2.b);
}
like image 664
monkeyking Avatar asked Feb 09 '13 14:02

monkeyking


People also ask

How do you initialize a new struct?

2 ways to create and initialize a new structThe new keyword can be used to create a new struct. It returns a pointer to the newly created struct. You can also create and initialize a struct with a struct literal. An element list that contains keys does not need to have an element for each struct field.

Can you initialize a struct?

When initializing a struct, the first initializer in the list initializes the first declared member (unless a designator is specified) (since C99), and all subsequent initializers without designators (since C99)initialize the struct members declared after the one initialized by the previous expression.

How do you initialize a struct variable in C++?

// In C++ We can Initialize the Variables with Declaration in Structure. Structure members can be initialized using curly braces '{}'.

Do structs have Initializers?

In Swift, types defined as structs automatically get a default initializer synthesized by the compiler — a so-called “memberwise initializer”, as the compiler will generate it based on the given struct's members (that is, its stored properties).


3 Answers

Give typeOuter an appropriate constructor:

struct typeOuter
{
  typeInner1 &one;
  typeInner2 &two;
  typeOuter(typeInner1 &a1, typeInner2 &a2) : one(a1), two(a2) {}
};



void fun(typeInner1 &arg1, typeInner2 &arg2) {
  typeOuter *ptr = new typeOuter(arg1, arg2);
  fun2(ptr);
}
like image 145
Angew is no longer proud of SO Avatar answered Nov 05 '22 14:11

Angew is no longer proud of SO


Before C++11, you will need a constructor for your struct typeOuter, and initialize the member references in the initializer list:

typeOuter(typeInner1& i1, typeInner2& i2) : one(i1), two(i2) {}

With C++11 you also have the option of using an initializer list directly (without defining a constructor yourself):

typeOuter *ptr = new typeOuter { arg1, arg2 };
like image 31
Mat Avatar answered Nov 05 '22 12:11

Mat


In C++ you can create a constructor for your struct. Structs are basically classes with public as default access modifier.

struct Example
{
    // struct fields..

    Example(); // initialize struct objects.
    ~Example(); // perform clean up if necessary.
};
like image 42
Tomislav Dyulgerov Avatar answered Nov 05 '22 12:11

Tomislav Dyulgerov