[cs13001] initializing complex structures and arrays of structures

Mikhail Nesterenko mikhail at cs.kent.edu
Thu Oct 18 16:52:29 EDT 2012


CSI students,

It turns out it is perfectly legal to initialize complex structures
and structures of arrays at declaration. Take a look at the below
example.

Thanks,
-- 
Mikhail

// initializing complex structures at declaration
// Mikhail Nesterenko
// 10/18/2012

#include <iostream>
#include <string>

using namespace std;

// example structure
struct example{
  int a;
  string b;
};

// second example structure whose member is a substructure
struct comp_example{
  int d;
  example e;
};

int main(){

  comp_example myStruct = {1, {2, "three"}};
  example myStructArray[2] = {{1, "one"}, {2, "two"}};

}


More information about the cs13001 mailing list