C Programming PowerPoint PPT Presentation

presentation player overlay
1 / 12
About This Presentation
Transcript and Presenter's Notes

Title: C Programming


1
C Programming
  • Structures and Unions

2
Structure
  • User-defined data type
  • Grouping of related data that makes programming
    easier
  • Begins with a structure definition
  • Uses the keyword struct

3
  • main()
  • struct
  • char initial / last name initial /
  • int age / childs age /
  • int grade / childs grade in school /
  • boy,girl
  • boy.initial 'R'
  • boy.age 15
  • boy.grade 75
  • girl.age boy.age - 1 / she is one year
    younger /
  • girl.grade 82
  • girl.initial 'H'
  • printf("c is d years old and got a grade of
    d\n",
  • girl.initial, girl.age, girl.grade)

4
Structure Examples
  • Each of the three elements of "boy" are simple
    variables and can be used anywhere in a C program
    where a variable of their type can be used.
  • The "age" element is an integer variable and can
    therefore be used anywhere in a C program where
    it is legal to use an integer variable, in
    calculations, as a counter, in I/O operations,
    etc.
  • It is illegal to use the name "boy" or "age"
    alone because they are only partial definitions
    of the complete field. Alone, the names refer to
    nothing.

5
Array of Structures
  • Define an array of structures to hold a group of
    related information for a certain number of
    entities
  • Keeps it easier to track records
  • Similar to storing data in a database
  • Easier to retrieve information for each data entry

6
  • main()
  • struct
  • char initial
  • int age
  • int grade
  • kids12
  • int index
  • for (index 0index lt 12index)
  • kidsindex.initial 'A' index
  • kidsindex.age 16
  • kidsindex.grade 84
  • kids3.age kids5.age 17
  • kids2.grade kids6.grade 92
  • kids4.grade 57

7
Pointers and Structures
  • Define a pointer that points to the structure
  • If we assign the value of the structure array
    variable to the pointer, it will point to the
    first element of the array.
  • Adding 1 to the pointer will cause it to point to
    the second field of the array.
  • If, for example, we were to add 4 to the pointer,
    it would advance the value of the pointer 4 times
    the size of the structure, resulting in it
    pointing 4 elements farther along the array.
  • Using "point-gtinitial" is the same as using
    "(point).initial" which is really the way we did
    it in the last two programs.
  • Remember that point is the stored data to which
    the pointer points and the construct should be
    clear.

8
  • main()
  • struct
  • char initial
  • int age
  • int grade
  • kids12,point,extra
  • int index
  • for (index 0index lt 12index)
  • point kids index
  • point-gtinitial 'A' index
  • point-gtage 16
  • point-gtgrade 84
  • kids3.age kids5.age 17
  • kids2.grade kids6.grade 92

9
Unions
  • A way to look at the same data with different
    data types
  • A way to use the same data with different names
  • A union allows you to store different types of
    data in the same physical storage locations.
  • This technique is often used to pack data bytes
    together when you are, for example, combining
    bytes to be used in the registers of the
    microprocessor.

10
Union Example
  • Suppose you wished to build a large database
    including information on many types of vehicles.
  • In order to keep all pertinent data, however, you
    would need those data points for their proper
    types of vehicles.
  • In order to build an efficient data base, you
    would need several different types of data for
    each vehicle, some of which would be common, and
    some of which would be different.

11
  • define AUTO 1
  • define BOAT 2
  • define PLANE 3
  • define SHIP 4
  • main()
  • struct automobile / structure for an
    automobile /
  • int tires
  • int fenders
  • int doors
  • typedef struct / structure for a boat or
    ship /
  • int displacement
  • char length
  • BOATDEF

struct char vehicle / what type of
vehicle? / int weight /
gross weight of vehicle / union
/ type-dependent data /
struct automobile car / part 1 of the
union / BOATDEF boat /
part 2 of the union / struct
char engines int wingspan
airplane / part 3 of the union
/ BOATDEF ship / part 4
of the union / vehicle_type int
value / value of vehicle in dollars
/ char owner32 / owners name
/ ford, sun_fish, piper_cub
/ three variable structures /
12
  • / define a few of the fields as an illustration
    /
  • ford.vehicle AUTO
  • ford.weight 2742 / with a
    full gas tank /
  • ford.vehicle_type.car.tires 5 / including
    the spare /
  • ford.vehicle_type.car.doors 2
  • sun_fish.value 3742 / trailer
    not included /
  • sun_fish.vehicle_type.boat.length 20
  • piper_cub.vehicle PLANE
  • piper_cub.vehicle_type.airplane.wingspan 27
  • if (ford.vehicle AUTO) / which it is in
    this case /
  • printf("The ford has d tires.\n",ford.vehic
    le_type.car.tires)
  • if (piper_cub.vehicle AUTO) / which it is
    not in this case /
  • printf("The plane has d tires.\n",piper_cub
    .vehicle_type.
  • car.tires)
Write a Comment
User Comments (0)
About PowerShow.com