Lecture Notes for Introduction to Computer Science II

29 May 2001 - Structures


  1. collect related data into groups

    1. inconvenient to manipulate

    2. easy to forget some parts

    3. keeping data from more than one group from mingling

  2. structs group related data

    1. syntax - struct struct-name { type name ; . . . };

    2. the data in a struct are called fields

    3. the field type can be anything

    4. all field names within the same struct must be different

    5. a struct may have any number of fields, including 0

  3. declaring structs

    1. a struct is like any other datatype

    2. variable declarations - struct-name var-name1, var-name2, . . ., var-namen;

    3. function declarations - struct-name1 f(struct-name2 param-name) { . . . }

  4. accessing fields

    1. use the dot operator . to access a struct's fields

    2. the fields are just like regular variables

    3. a field must be accessed through a struct

  5. using structs

    1. stucts may be passed around like any other variable

    2. you can, eventually, treat structs like any other variable

  6. the struct definition must appear in the same file as field accesses


This page last modified on 29 May 2001.