Docsity

Structs and Enumerations: Understanding Data Structures in C and C++, Slides of Computer Engineering and Programming

Partial preview of the text

Download Structs and Enumerations: Understanding Data Structures in C and C++ and more Slides Computer Engineering and Programming in PDF only on Docsity!

Structs and Enumeration

Lecture 23

Data Structures (struct)

• Arrays require that all elements be of the

same data type. Many times it is necessary to

group information of different data types. An

example is a materials list for a product. The

list typically includes a name for each item, a

part number, dimensions, weight, and cost.

• C and C++ support data structures that can

store combinations of character, integer

floating point and enumerated type data.

They are called a structs.

Declaring Structures (struct)

Reserves Space

struct

my_example

int label;

char letter;

char name[20];

} mystruct ;

Does Not Reserve Space

struct my_example

int label ;

char letter ;

char name[20] ;

/* The name "my_example" is

called a structure tag */

User Defined Data Types (typedef)

  • The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration:

typedef int Length; makes the name Length a synonym (or alias) for the data type int.

  • The data “type” name Length can now be used in declarations in exactly the same way that the data type int can be used:

Length a, b, len ; Length numbers[10] ;

Accessing Struct Members

  • Individual members of a struct variable may be accessed using the structure member operator (the dot, “.”): mystruct.letter ;
  • Or , if a pointer to the struct has been declared and initialized

Some_name myptr = &mystruct ; by using the structure pointer operator (the “->“): myptr -> letter ; which could also be written as: (myptr).letter ;

Sample Program With Structs

/* This program illustrates creating structs and then declaring and using struct variables. Note that struct personal is an included data type in struct "identity". */ #include <stdio.h> struct personal //Create a struct but don’t reserve space. { long id; float gpa; } ; struct identity //Create a second struct that includes the first one. { char name[30]; struct personal person; } ;

Structs with Union

• /* The program on the next 3 slides creates a

union and makes it a member of struct

personal which is, in turn, a member of struct

identity. The union uses the same memory

location for either rank or a character string

(deg) depending on the answer to the prompt

for student status in main( ) */

Structs with Union (cont.)

#include <stdio.h>

union status

{

int rank ; char deg[4] ;

} ;

struct personal { long id ; float gpa ; union status level ; } ;

struct identity { char name[30] ; struct personal student ; } ;

Structs with Union (cont.)

else { printf ("Enter degree sought -- ms or phd\n"); scanf ("%s", &jb.student.level.deg); printf ("%s is a %s candidate\n”, jb.name , jb.student.level.deg ); } /* End of else statement */ printf ("%s %ld %f\n” , jb.name , jb.student.id , jb.student.gpa ); printf ("%s%ld %f\n” , ptr->name , ptr->student.id , ptr->student.gpa );

} /* End of program */

Enumeration

  • Enumeration is a user-defined data type. It is defined using the keyword enum and the syntax is: enum tag_name {name_0, …, name_n} ;
  • The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. As an example, the statement: enum colors { red, yellow, green } ;
  • creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2.

Enumeration

enum week {week_one, week_two,

week_three,

week_four, week_five};

printf ("Monday the third week "

"of March is March %d\n",

March [week_three] [Monday] );