0

I'm trying to define a struct with other structs as members of that struct but I'm not sure how this is done. The code I have looks like this so far:

typedef struct name
{

    char fname[15];
    char lname[15];

} Name;

typedef struct info
{

    int grade;
    char phone[13];

} Info;

typedef struct mark
{

    int math;
    int sci;
    int eng;

} Mark;

typedef struct student
{

    Name n;
    Info i;
    Mark m;

} Student;

int main()
{

    Student class_list[30] = { };

}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Brett Morris
  • 27
  • 2
  • 6
  • 2
    That looks like C, not C++. Which language do you actually use? – Timo Apr 03 '19 at 23:55
  • 1
    `typedef struct` -- This is not necessary in C++. Just `struct` is all you need. – PaulMcKenzie Apr 03 '19 at 23:57
  • @Timo I use C++ mostly I believe, I think we may use some of both in our coding though. I'm using this code for a grade 12 computer science project I have. Either way, I'm just trying to figure out how to define this. – Brett Morris Apr 04 '19 at 00:03
  • what are you trying to achieve? What is the problem with the code? Your question doesn't include a question – bolov Apr 04 '19 at 00:08
  • Looks to me like you have it . `student` has a `Name`, an `Info`, and a `Mark` and everything compiles. For syntax questions you should start with [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) rather than Stack Overflow. There's going to be stuff you don't quite grok, but it looks like you did get this and a non-fraudulent text would have told you that a lot quicker than typing all that stuff in. – user4581301 Apr 04 '19 at 00:09
  • @bolov yes, I'm wondering how to define the struct 'class_list' at the end of the code, including my structs I have made. – Brett Morris Apr 04 '19 at 00:10
  • Ah-ha! I may have misread your intent. You have defined an array of `student`s, but are you asking how to initialize the values? – user4581301 Apr 04 '19 at 00:11
  • ah yes, sorry for the confusion – Brett Morris Apr 04 '19 at 00:12
  • You just defined the variable. So the answer is ... just like you did – bolov Apr 04 '19 at 00:12
  • Yes, that is how you define struct as member of struct.Though, those typedefs are redundant. – qqqqq Apr 04 '19 at 00:16

1 Answers1

2

If your question is how to initialize the array then the answer is like this:

Student class_list[2] = {
    {{"John", "Doe"}, {8, "000-555-000"}, {1, 2, 3}},
    {{"Jane", "Doe"}, {10, "000-555-001"}, {10, 8, 10}},
};

The reasons it works like this is because your classes are aggregates so you can use aggregate initialization.

Well, I initialized an array of 2 elements. You can see the syntax.


Some points for your program. The way of typedef struct is a C idiom. In C++ you don't need it so please change all the definitions to:

struct Name
{
    char fname[15];
    char lname[15];
};

Also you should use std::array instead of C arrays:

std::array<Student, 3> class_list {{
    {{"John", "Doe"}, {8, "000-555-000"}, {1, 2, 3}},
    {{"Jane", "Doe"}, {10, "000-555-001"}, {10, 8, 10}},
}};
bolov
  • 72,283
  • 15
  • 145
  • 224