Lecture overview -- Keyboard shortcut: 'u'  Previous page: Arrays and Pointers: Examples -- Keyboard shortcut: 'p'  Next page: Structures: Examples -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 1 - Page 16 : 29
Notes about C++
From C to C++
Structures and Unions

A struct aggregates fields of different types to a new composite type

A union is the poor man's - the C programmer's - way of specializing a struct in two or more directions

  • Struct basics

    • Consequtive memory (maybe slighly more than needed), fields of different types

    • Direct component selection str.field - dot notation

      • Also indirect component selection from a pointer to a struct: str_ptr->field

    • Bit fields: Access to selected bits in a field of integer type

  • Union basics

    • A structure where all fields occupy the same memory

    • No way to inquire about the actual type of contents

    • Can be (mis)used to discover details about the representation of data

  • Value semantics

    • Copied when passed as parameters

    • Copied when returned from function

    • If value semantics is too costly, pointers to structs can be used

The C struct is the major starting point of the C++ abstraction mechanisms