Docsity

Structures and Unions-Introduction to Programming-Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download Structures and Unions-Introduction to Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Dr. Hanif Durad

Lecture Outline

Structures

User Defined Data Types (typedef)

Unions

Enumeration (enum)

Dynamic memory allocation

Structures (struct)

^

Arrays require that all elements be of the same datatype. Many times it is necessary to groupinformation of different data types. An example is amaterials list for a product. The list typicallyincludes a name for each item, a part number,dimensions, weight, and cost.

^

C and C++ support data structures that can storecombinations of character, integer floating point andenumerated type data. They are called a

structs

Dr. Hanif Durad

D:\C Language\Hanif Search\Structures\Lecture 23 - Structs and Enumeration - 06.ppt

Declaring Structures (struct)

Does Not Reserve Space struct my_example{

int label;char letter;char name[20]; } ; /* The name "my_example" is

called a structure tag

Reserves Space struct my_example{

int label;char letter;char name[20]; } mystruct Dr. Hanif Durad

Structure Example-1 (1/2)^ #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; } ;

Dr. Hanif Durad

struct1.c

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 ;

Dr. Hanif Durad

Structure Example-2 (1/3)^ /* Demonstrates using arrays of structures. /#include <stdio.h>/ Define a structure to hold entries. */struct entry {char fname[20];char lname[20];

char phone[12];}; /* Declare an array of structures. */struct entry list[3];int i;

Dr. Hanif Durad

phone_struct.c

Teach Yourself in 21Days/c/ch11/ch11.htm

Structure Example-2 (3/3)

/* Print two blank lines. /printf("\n\n");/ Loop to display data. */for (i = 0; i < 3; i++){

printf("Name: %s %s", list[i].fname, list[i].lname);printf("\t\tPhone: %s\n", list[i].phone); } return 0; }

Dr. Hanif Durad

User Defined Data Types(typedef) (1/3)

^

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] ;

Dr. Hanif Durad

User Defined Data Types(typedef) (3/3)

typedef struct t_name{ char last_name[25];char first_name[15];char middle_init[2];} NAME;...NAME my_name, your_name;... your_name = my_name; ... In the preceding example, the entire contents of the my_name structure were copied into the

your_name structure. This is essentially the same as the following line: memcpy(your_name, my_name, sizeof(your_name));

D:\C Language\C Programming - Just the FAQS – PDF\01 - The C Language.pdf

Unions (1/2)

^

A union is an object that can hold any one of a set of namedmembers. ^

The members of the named set can be of any data type. ^

Members are overlaid in storage. ^

The storage allocated for a union is the storage required for thelargest member of the union, plus any padding required for theunion to end at a natural boundary of its strictest member. ^

Unions are typically used to save space, by not storing allpossibilities for certain data items that cannot occur together http://lectures-c.blogspot.com/2009/02/structures-and-union.html

Prentice Hall - Expert C Programming.pdf, P-

Union Example (1/2)^ /*An example of a union /#include <stdio.h>/ number union definition */union number {

int x;double y; }; /* end union number */

Dr. Hanif Durad

union_examp.c

Union Example (2/2)

int main( void ){

union number value; /* define union variable /value.x = 100; / put an integer into the union */printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n\n\n“, "Put a value in the integermember", "and print both members.",

"int:", value.x, "double:", value.y );value.y = 100.0; /* put a double into the same union / printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n“, "Put a value in the floatingmember“, "and print both members.“, "int:", value.x, "double:", value.y );return 0; / indicates successful termination / } / end main */

Dr. Hanif Durad

enum Example (1/2)// enum2.c// demonstrates enum types#include <stdio.h>//specify enum typeenum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };int main()

{ int day1, day2, diff; //define variables

//of type days_of_week

day1 = Mon;

//give values to

day2 = Thu;

//variables

Dr. Hanif Durad

enum2.c

Modified fromRobert Lafore C++

enum Example (2/2)diff = day2 - day1;

//can do integer arithmetic

printf ("Days between = %d \n", diff);if(day1 < day2)

//can do comparisons

printf ("day1 comes before day2\n");return 0;}

Dr. Hanif Durad