0

I have the following code and I need to initialize the XYZ struct. Please give me a way to do this.

struct Demo
{
    int abc[10];
    int reply_port;
};

struct XYZ 
{
    struct Demo dem[10];
    int x;
    int y;
    struct Demo a;
    struct Demo b;
    struct Demo c;
} arr[100];

Demo1 is another struct that is available in other file. Please tell me an efficient way in how this struct XYZ can be initialized.

Thanks

4
  • What do you want to initialize it to? All 0s? Commented Dec 4, 2013 at 20:12
  • How do you want to initialize the structs? There are different kinds of initialization. Commented Dec 4, 2013 at 20:13
  • If you ask a question about C++, don't tag C. The modern languages can be very different. Commented Dec 4, 2013 at 20:15
  • Can you use c++11 features? Commented Dec 4, 2013 at 20:22

3 Answers 3

2

I don't know why do you want to do it this way, but if you insist:

  struct XYZ myXYZ = { 
    {   
      // dem[10]
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80},
      {{1,2,3,4,5,6,7,8,9,10},80}
    },  
    10, // x
    20, // y
    {{1,2,3,4,5,6,7,8,9,10},80}, // a
    {{1,2,3,4,5,6,7,8,9,10},80}, // b
    {{1,2,3,4,5,6,7,8,9,10},80}  // c
  };

I would think of designing it differently, but I don't know what problem you're trying to solve.

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder why magic numbers from 1 to 10 and then 80 are of use, but the author of the question seems not to be interested in discourse
The numbers are just an example. Why 80? In his structure he has reply_port in that position in the struct, and 80 is one of the most widely used ports today on the Internet :), hence I chose it as an example.
1

Init to all 0's

XYZ xyz = {};

No init: http://ideone.com/wE7MCF → undefined behavior

Init: http://ideone.com/BT81PH → all 0s

Comments

0

Why won't you use class? Further in the class you may define a parameterized constructor.

Or do you want to do it with structures only?

6 Comments

In this context, class and struct make no difference in C++.
Going by the definitions I tend to keep structures responsible only for data items. I restrain myself from using classes till its not entirely necessary.
Right, but both things are the same in C++. You can define exactly the same types using either.
Yes. I have had the experience of that while working on one project when my professor shared the same thing with me. C++ compilers tend to assume structures as classes. There remains no difference but to avoid confusion in other languages I like to keep things clear. :)
And you can define a constructor for the struct: Demo() : reply_report(0) { memset(...) }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.