Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:29:54+00:00 2026-06-18T12:29:54+00:00

Is it possible to have a struct containing references to structs. And how are

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:29:55+00:00Added an answer on June 18, 2026 at 12:29 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: struct Mystruct { void Update(float Delta); } typedef std::map<int, Mystruct*> TheMap; typedef
I have a struct that takes a function pointer, like this: typedef int (*node_transition_func)(
Possible Duplicate: Forward declarations of unnamed struct If I have typedef struct tagPAGERANGE {
is possible to have a separator between elements of a GridView? Thanks
I was wondering if it was possible to create a struct containing a number
Is it possible in C# to have a Struct with a member variable which
I have a struct as defined by the following: typedef struct { NSString *SportName;
I have a struct called CoolStruct: struct CoolStruct { int id; uint32 type; uint32
Possible Duplicate: Send and receive NSData via GameKit I have struct which consists of
Is it possible to have mutual recursive types ( [<Struct>] ) spread across different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.