Tags: %TAGME{ tpaction="" web="SCMP118/Fall2016" tag="" }% view all tags

Notes on Section 10.1


  • Structure Tag

  • Member Names

  • Structure Value

  • Member Values

  • Member Variables


struct AcadRecord  // Structure definition
     int id_number;
     double units;
     double gpa;
     int semesters;
AcadRecord stu_rec;  //Structure declaration
stu_rec.gpa = 2.45;   //Member variable assignment

AcadRecord create_rec(int num, double total_units, double total_gpa, int semesters)
		AcadRecord student;
		student.id_number = num;
		student.units = total_units;
		student.gpa = total_gpa;
		student.semesters = semesters;
		return student;


  • Define structures globally, but declare them locally.

  • DO NOT forget the semicolon after the structure definition. It's very easy to do.

  • You may use the same name for a member variable in different structures.

  • You may assign one structure's value to another structure. This has the effect of assigning all member variables one-by-one. (So if I had two structures of type AcadRecord, call them stu_1 and stu_2, and I used the assignment command stu_1=stu_2, this would have the effect of assigning stu_1.id_number the value that is in stu_2.id_number, assigning stu_1.units the value that is in stu_2.units, etc.

  • A function may return a structure, just as functions can return ints, chas, etc.

  • To initialize a structure all at once, list the values the member variables are to take on in braces: stu_rec = {69302, 8.25, 2.75, 4}

  • A structure may have another structure as a member variable. If I wanted to define a structure KenStudent, I might have a member variable of type char for gender ('F' or 'M'), a member variable string for the name, and a member variable of type AcadRecord for the student's academic record, for example.

Topic revision: r1 - 2015-11-01 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback