Thursday, November 3, 2011

Array of Struct Initialization

#include <iostream>
#include <string>
using namespace std;


// A struct Definition
struct Student
{
  string name;
  int id;
  float gpa;
}; // Student

int main()
{
  Student a[2]={{"Tom",123455, 3.66},
                {"Tom",123455, 3.66}};
  a[0].name = "George Washington";
  a[0].gpa = 3.14;
  a[0].id = 123456;

  cout << "Name=" << a[0].name
       << ", ID=" << a[0].id
       << ", GPA=" << a[0].gpa << endl;

  cout << "Name=" << a[1].name
       << ", ID=" << a[1].id
       << ", GPA=" << a[1].gpa << endl;

  return 0;
}

No comments:

Post a Comment