You can refer to individual fields of stu1 and stu2

This preview shows page 7 - 15 out of 33 pages.

 are two separate variables, each with 5 fields. You can refer to individual fields of  stu1 and stu2  as follows: strcpy(stu1.name, "Umay Ece Tuğcu"); stu1.id=123; strcpy(stu2.name, "Bilge Ada Tuğcu"); stu2.id=456; Then, the variables will be as follows: name id dept class gpa stu1 name id dept class gpa stu2 name id dept class gpa stu1 "Umay Ece Tuğcu" 123 name id dept class gpa stu2 "Bilge Ada Tuğcu" 456
Image of page 7

Subscribe to view the full document.

TT - Spring'08 CMPE150: Introduction to Computing 8 Defining struct Variables There are two ways of defining a struct variable: Define the variable while defining the structure. struct { char name[21]; int credit; } cmpe150, cmpe322; /*Define both type and variables*/ Note that the optional struct name has been omitted. Define the variable after  the structure has been defined. struct course_type { char name[21]; int credit; }; /*Define only the type*/ struct course_type cmpe150, cmpe322; /*Define vars*/ Note that the optional struct name cannot be omitted here since we need the struct name  later to define variables of that type. Also, note that you cannot simply say  course_type ; you have to say  struct course_type .
Image of page 8
TT - Spring'08 CMPE150: Introduction to Computing 9 struct as a Field of Another struct You may have a struct that has a field of struct type. Eg: struct A_type { int m, n; }; struct B_type { int f; struct A_type g; } t; t  has two fields:  f  and  g .    f  is of type  int g  is of type  struct A_type Thus,  t  has the following fields (and subfields): t.f t.g t.g.m t.g.n
Image of page 9

Subscribe to view the full document.

TT - Spring'08 CMPE150: Introduction to Computing 10 Example #2 Define a type for a point in two-dimensional space. struct point_type { int x, y; };
Image of page 10
TT - Spring'08 CMPE150: Introduction to Computing 11 Example #3 Define types for a triangle and a rectangle. struct triangle_type { struct point_type A, B, C; }; struct triangle_type t={{1,3},{2,4},{1,6}}; struct rectangle_type { struct point_type A, B, C, D; };
Image of page 11

Subscribe to view the full document.

TT - Spring'08 CMPE150: Introduction to Computing 12 Example #4 Define a type for a cube. struct point3_type { int x,y,z; }; struct cube_type { struct point_type c1,c2,c3,c4,c5,c6,c7,c8; }; Can you find a better  representation for a cube?
Image of page 12
TT - Spring'08 CMPE150: Introduction to Computing 13 Initializing struct Variables You may initialize struct variables during definition (in a way  similar to arrays). struct A_type { int m, n; } k={10,2}; struct B_type { int f; struct A_type g; } t={5,{6,4}}; The following kind of initialization is wrong . struct A_type { int m=10, n=2; } k;
Image of page 13

Subscribe to view the full document.

TT - Spring'08 CMPE150: Introduction to Computing 14 Initializing struct Variables You cannot use the  {...}  format for initialization  after  initialization (just as in arrays), ie,  struct A_type { int m, n; } k; ...
Image of page 14
Image of page 15
You've reached the end of this preview.
  • Spring '09
  • TT
  • Long integer, Short integer, struct, Umay Ece Tuğcu

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern