Structures - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Structures

Description:

Structs are a way of storing a collection of related data together as a single ... binding operator has a higher precedence than the dereference operator, which ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 15
Provided by: alanb9
Category:
Tags: structures

less

Transcript and Presenter's Notes

Title: Structures


1
Structures Unions
  • Chapter 18

2
Structures
  • Structs are a way of storing a collection of
    related data together as a single unit, much like
    an object in Java.
  • Unlike objects, structs only contain data, they
    do not have member functions to manipulate the
    data.
  • Each piece of data in a structure is called a
    field or member.

3
Structure Templates
  • A structure template is a type definition that
    defines the fields of a struct.
  • typedef struct field declarations type_name

4
An Example
  • typedef struct char first_nameMAX_SIZE char
    last_nameMAX_SIZE char middle_initial
  • short num_children char ssn12
    person_tint main() person_t someone return
    0

5
Accessing Struct Fields
  • To access the data of a structure, use the
    structure variable name followed by the structure
    binding operator, . and then the field name.
  • printf(s c. s, someone.first_name,
    someone.middle_initial, someone.last_name)
  • someone.num_children2

6
Copying Passing Structures
  • You can copy structures just like you would any
    other simple data type
  • person_t person1, person2, temptemp
    person1person1person2person2temp
  • You can also pass structures by value to your
    functions. Try to avoid passing by value when
    possible, creating a copy of the struct with all
    of its data can be a very time consuming
    operation depending on the size and amount of
    data in the structure.

7
Pointers to Structures
  • Creating a pointer to a struct is no more
    difficult than with any other data type.
  • person_t ptr, person1ptr person1
  • Using the . operator with a struct pointer is a
    little more difficult. The structure binding
    operator has a higher precedence than the
    dereference operator, which means a statement
    like ptr.num_children2 would try to
    dereference the num_children field of the
    structure, rather than the structure itself. You
    need to use this instead (ptr).num_children2
  • To eliminate the complexity of the parentheses,
    there is a special operator for structure pointer
    variables -gt which automatically dereferences
    the pointer correctly without the extra
    parentheses.
  • ptr-gtnum_children2 //this statement is
    equivalent to // (ptr).num_children2

8
Use of Structs
  • While its perfectly ok to declare structs right
    inside your .c file and access their fields
    throughout your code using the binding operator,
    there is a better way to do it.
  • If theres a possibility that you may want to use
    your struct again in the future, its a good idea
    to define it in a header file, then include that
    header in any C program you want to use it in.
    Its also a good idea to write a set of member
    functions for setting fields and getting fields
    of the struct. This allows for encapsulation, so
    you (or someone else using your struct) doesnt
    need to know the exact layout of the struct.
    Once you have member functions, you can create a
    simple API to remind yourself or others of how to
    use your struct properly.

9
Nesting Structures
  • Your structs can contain other structs as
    members.
  • typedef struct char nameMAX_SIZE char
    type_of_animalMAX_SIZE pet_ttypedef
    struct char first_nameMAX_SIZE char
    last_nameMAX_SIZE char middle_initial
  • short num_children pet_t pet
    person_tint main() person_t someone
  • printf(pet name s, someone.pet.name)
  • return 0

10
Self Referencing Structures
  • Sometimes its necessary for a structure to
    contain, or have a pointer to a structure of the
    same type. This is useful for things like linked
    lists, graphs, and trees where each node needs to
    reference at least one other node.
  • In the way weve defined our structures so far,
    we havent named them until the end of the struct
    definition, thus the compiler doesnt know about
    the type when it encounters it as a field.
    Instead we need to do a typedef first, then
    define our struct.

11
Illustrating the Problem
  • typedef struct char first_nameMAX_SIZE char
    last_nameMAX_SIZE char middle_initial
  • short num_children char ssn12 person_t
    spouse
  • person_t

12
The Solution
  • typedef struct person person_t
  • struct person char first_nameMAX_SIZE char
    last_nameMAX_SIZE char middle_initial
  • short num_children char ssn12 person_t
    spouse

13
Initializing Structures
  • You can initialize structures using the
    operators.
  • Given our original person struct, you could
    initialize it like soperson_t someone
    Joe, Schmo, D,2,003-11-0002

14
Unions
  • A union is a block of memory with multiple names
    and data types. With structures, each field has
    its own memory location, with unions all of the
    fields share the same memory location.
  • Can be used to subdivide a large object into
    pieces, or to conserve memory.
  • typedef union double number char bytes8
    split_double_t
  • In this example, you can access the whole double,
    or you can read it one byte at a time by using
    the bytes field instead.
  • To save memory using unions, you could have a
    structure with two mutually exclusive fields one
    of the fields will always be used, but never both
    at the same time. You could create a union for
    those two fields so they only take one segment of
    memory rather than the two that would be required
    if you didnt declare them as a union.
Write a Comment
User Comments (0)
About PowerShow.com