22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access...

35

Transcript of 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access...

Page 1: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.
Page 2: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-2

• Understand how structure data types are implemented in C.

• Use “.” operator to access members of a structure.

• See how to declare pointers to structure variables.

• Use “ -> ” operator to access members of a structure when using pointers.

• Related Chapter: ABC 9.1 – 9.6

Page 3: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-3

1. Problem Definition

Write a program that keeps an inventory for an auto parts store. Previously, the information for each auto part was kept on an index card. Each index card had the following fields:

id --- a unique identifier for your parts name ---- a category name for the part, like “air filter” quantity --- the quantity in stock price ---- retail price

The auto parts store can have as many as 30,000 different parts.

Your program should define a new C data type named “part”. You will declare 30,000 variables of data type “part”. Each variable (like an index card) should hold the values for all of the fields listed above.

Page 4: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-4

2. Refine, Generalize, Decompose the problem definition

(i.e., identify sub-problems, I/O, etc.)

We first define a new data type “part”. Then in main we will declare an array of 30,000 variables each of data type “part”.

Your program will display a menu with the following choices:

0. Exit 1. Display the parts.

2. Find a part.3. Enter information for a new part.

For each menu item we will write a corresponding function to carry out that menu option.

Input = A choice from the menu. For menu choice 2. the user must supply the part “id”. For choice 3. the user must supply all the information for the new auto part.

Output= For choice 1. all the fields of all the parts will be displayed on the computer screen.

Page 5: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 30000

typedef struct { /* user defined data-type called part */ int id; /* this is a definition*/

char name[32]; /* no variables are declared */ int quantity; /* location is outside any block */

float price;} part;

int menu(void){ int choice; /* C merges strings */ printf("\n0. Exit \n" "1. Display the parts.\n"

"2. Find a part.\n" "3. Enter information for a new part.\n"

"choice: "); scanf("%i",&choice); return choice;} /* end of menu */ /* continued on next slide */

Page 6: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

void display_one_part(part p){ printf(“%-9i %-31s %-5i $%.2f \n”, p.id

, p.name , p.quantity , p.price);

} /* end of display_one_part */

int display_parts(part parts[],int count){ int i; for(i = 0; i < count; ++i) display_one_part(parts[i]);} /* end of display_parts */

/* continued on next slide */

Page 7: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

int find_part(part parts[], int count){ int i, id_sought; /* return the index of the array for given id */ /* return the value -1 if id_sought not found */ printf("Enter an id number: "); scanf("%i",&id_sought); for(i= 0; i < count; ++i) if (id_sought == parts[i].id) return i; return -1;} /* end of find_part */

/* continued on next slide */

Page 8: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

int add_part(part parts[],int count, int max_size){ int i;

if (count == max_size) { printf(“insufficient space to add another part\n”); return count; }

/* use C regular expression [ ] to read a string with blanks*/ /* ^ means NOT */ printf("Enter one part in format \n" printf(" id; part name ; quantity; price \n"); scanf(" %i; %[^;]; %i; %f", &parts[count].id

, parts[count].name , &parts[count].quantity , &parts[count].price); return count+1;} /* end of add_part */

/* continued on next slide */

Page 9: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

void main(void){ int choice, index; part parts[MAX_SIZE]; /* MAX_SIZE is a constant = 30000 */ int count = 0; /* actual number of parts in array */ while(1) /* infinite loop */ { choice = menu();

switch(choice){

case 0: printf("Bye bye!\n"); return;

case 1: display_parts(parts,count); break;

case 2: index = find_part(parts,count); if ( index != -1)

display_one_part(parts[index]); else

printf("Part not found!\n"); break;

case 3: count = add_part(parts,count,MAX_SIZE); } /* end of switch */ } /* end of while */} /* end of main */

Page 10: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-10

(see demo in lecture)

Page 11: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-11

To set up a structure in C, we need to

(1) Define a structure type. There are several ways to do this in C. In general, we will use the typedef mechanism to

do this.

(2) Declare variables to be of that type.

Note: The built-in data types (e.g. int, float, char,…) can be used to declare variables in the C language. These data types don’t have to be defined by the user.

Structures provide the user with the means to define custom designed data types.

Page 12: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-12

You can use the typedef mechanism in C to create an alias for a structure.

The code above names “part” as a alias for,

typedef struct { int id;

char name[32];int quantity;

float price;} part;

struct { int id;

char name[32]; int quantity;

float price;

};

Page 13: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-13

Structure definitions do not reserve storage, they are used to define a new data type.

For example, the code above doesn’t allocate any storage. It doesn’t reserve memory for id, name, quantity or price --- the fields or members of the structure.Also, part is not a variable its like int, char ,float, double.

typedef struct { int id;

char name[32];int quantity;

float price;} part;

Page 14: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-14

Once you have defined a new data type, you can declare (and initialize) a variable of this data type as in,

The variables p is declared to be of data type “part” where “part” is a synonym for the structure,

part p ={1001,”Air Filter”, 100, 12.49};

Structure variables, like p above, are initialized in the same way you initialize an array.

struct { int id;

char name[32];int quantity;

float price;};

Page 15: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-15

p

.id 1001

12.49

“Air Filter”

part p ={1001,”Air Filter”,100, 12.49};

After the declaration of p, the picture of memory looks like,

.name100.quantity

.price

1000

Ad

dre

ss1040

1004

1036

typedef struct { int id;

char name[32];int quantity;

float price;} part;

Page 16: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-16

part parts[30000];

We can even declare an array of structure data type.

typedef struct { int id;

char name[32];int quantity;

float price;} part;

Page 17: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-17

parts[0]

.id

Here is a memory map showing the first two element in the array named parts.

.name.quantity.price

2000

Ad

dre

ss

2040

2004

2036

parts[1]

.id.name

.quantity.price

2044

2084

2048

2080

Page 18: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-18

struct Part p = {1001,”Air Filter”, 100, 12.49};

A second, alternative method in using structures in C is to code the following immediately after the preprocessor directives as in slide 5,

The name Part is called a “tag name”.In order to declare variable p you would write,

struct Part { int id;

char name[32];int quantity;

float price;};

Page 19: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-19

A third, alternative method in using structures in C is to combine the struct and declaration of the variable in one statement.

struct Part{ int id;

char name[32];int quantity;

float price;} p = {1001, "Air Filter", 100, 12.49};

Page 20: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-20

Structures provide the mechanism in C for grouping values of different data types.Example:

typedef struct {int employeeID;float salary;int departmentID;char name[30]; /* not same as char * name; */

} Employee;

Page 21: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-21

Accessing(storing and fetching) values from/to fields in the structure is accomplished with the "dot operator" (.), also called the "structure member operator ".Example: (From slide 14)

p.id = 1001; /* use the . to access individual fields */p.quantity = 100;

Example: (From the previous slide)

Employee emp1; /* declare a variable of type Employee */

emp1.employeeID = 1004; /* initialize the variable */

emp1.salary = 45123.50;

Page 22: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-22

scanf("%f",&emp1.salary); /* you can also use scanf */

emp1.departmentID = 37;

strcpy(emp1.name, "John");/* strcpy function */

scanf("%s",emp1.name); /* no & needed */

emp1.name[0] = ‘J’; /* this also works */

emp1.name[1] = ‘o’;

emp1.name[2] = ‘h’;

emp1.name[3] = ‘n’;

emp1.name[4] = ‘\0’;

Page 23: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-23

display_one_part(parts[i]);

Function calls with an argument having a structure data-type are implemented as call-by-value. In this regard structures differ from arrays in that arrays are passed as call-by-reference.

For example, in slide 6, function display_one_part is called with argument “parts[i]” of data-type part.

The header for the function display_one_part , void display_one_part(part p)

has one parameter, p . The variable p is “local” in scope to the function display_one_part. In call-by-value the value of parts[i] is copied into p. p is not a synonym for parts[i].

Page 24: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-24

part find_part(part parts[], int count){ int i, id_sought; printf("Enter an id number: "); scanf("%i",&id_sought); for(i= 0; i < count; ++i) if (id_sought == parts[i].id) return parts[i];} /* end of find_part */

Functions can return values of structure data-type. For example, in slide 7, we could modify the find_part function,

so that it returns the actual part. Of course the above code won’t work if the user types an invalid part id. Also, the call to find_part in main would have to be modified in order for the program to work.

Page 25: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-25

1. Problem Definition

Write a program that reads in data for the elements in the periodic system, (e.g. Hydrogen, Helium,...) the name as a string , symbol as a string, atomic number as an int and atomic weight as a double. After the elements have been entered they are sorted using qsort in ascending alphabetical order and then printed back to the terminal.

2. Refine, Generalize, Decompose the problem definition

(i.e., identify sub-problems, I/O, etc.)

We have four values per atomic element:

name,symbol,atomicNum and atomicWt

Rather than using five separate arrays of 118 elements we will

use one array AtomicElement of 118 elements. The data-type of

AtomicElement is a user-defined structure named Element.

Input = Input data from keyboard as above.

Output= Sort the elements by atomic name, using qsort.

Page 26: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

#include <stdio.h>#include <stdlib.h>typedef struct { /* user defined data-type called Element */

char name[15]; /* this is a definition, no variables are declared */char symbol[3];/* location of this definition is outside any block */int atomicNum;

double atomicWt;} Element;

int cmpnames(Element * ptr1, Element * ptr2) ; /* prototype *//* see explanation of the above in subsequent slides */void main(void){ int num, i; Element AtomicElement[118]; /* AtomicElement is an array */

printf("How many elements are you going to enter? "); scanf("%i",&num); for(i=0;i<num;++i) { scanf("%s %s %i %lf",AtomicElement[i].name

,AtomicElement[i].symbol ,&AtomicElement[i].atomicNum ,&AtomicElement[i].atomicWt);

}

Page 27: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

/* sort the arrays by name */

qsort(AtomicElement, num, sizeof(AtomicElement[0]), cmpnames);

/* print the sorted array */

printf("\n");

for (i=0;i<num;++i) { printf("%s %s %i %.5lf ",AtomicElement[i].name

,AtomicElement[i].symbol ,AtomicElement[i].atomicNum ,AtomicElement[i].atomicWt);

printf("\n"); /* print one element per line */

} /* end of for */

} /* end of main */

int cmpnames(Element * ptr1, Element * ptr2)

{

return strcmp(ptr1->name, ptr2->name); /* why arrow and not dot? */

} /* end of cmpnames */

Page 28: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-28

> ./a.out

How many elements are you going to enter?4

Hydrogen H 1 1.00794

Beryllium Be 4 9.01218

Gold Au 79 196.96655

Carbon C 6 12.0107

Beryllium Be 4 9.01218

Carbon C 6 12.01070

Gold Au 79 196.96655

Hydrogen H 1 1.00794

input

output

Page 29: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-29

strcmp(ptr1->name, ptr2->name)

If the string ptr1->name < ptr2->name (alphabetically)then strcmp(ptr1->name, ptr2->name) has a negative value

else if ptr1->name > ptr2->name then strcmp(ptr1->name, ptr2->name) has a positive value

else if ptr1->name == ptr2->name then strcmp(ptr1->name, ptr2->name) has the value 0. These

are the correct values we want to return to qsort and we do this by

From Lecture 15 slide 21:

strcmp(str1, str2) - returns a negative , zero or positive int depending on whether str1 is alphabetically less than, equal or greater than str2 respectively.

qsort calls our function cmpnames and passes two addresses which are assigned to the pointer variables, ptr1 and ptr2.

return strcmp(ptr1->name, ptr2->name);

Page 30: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-30

Address 2030

prt1 ptr2 AtomicElement[0]

“Beryllium”

Address 2000

20302000

“Be” 4 9.01218

“Hydrogen” “H” 1 1.00794

AtomicElement[1]

When qsort calls ‘cmpnames’

Page 31: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-31

Address 2030

prt1 ptr2 AtomicElement[0]

“Hydrogen”

Address 2000

20302000

“H” 1

9.01218“Beryllium” “Be” 4

1.00794

AtomicElement[1]

After qsort calls ‘cmpnames’

Since ‘cmpnames’ function returns a positive value to the function ‘qsort’. The function ‘qsort’ does the work ofswapping the 30 bytes of AtomicElement[0] withthe 30 bytes of AtomicElement[1].

Page 32: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-32

We can declare an array of the struct data type Autopart.

AutoPart structArray[50];

declares structArray as an array which contains 50 cells (in memory) of type AutoPart.

typedef struct {int id;char name[30];

} AutoPart;

Given the data-type definition:

Page 33: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-33

We can access the field in the structure with pointers and arrow operator “ -> ” (also called the indirect component selection operator ). The general format of the use of the arrow operator is:

(see example on next slide)

pointer_to_structure -> member_name

Page 34: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-34

typedef struct {int employeeID;float salary;int departmentID;char name[30];

} Employee;

Employee emp1,emp2;Employee *empPtr; /* pointer to data-type Employee */emp1.employeeID = 1004;emp1.salary = 45123.50;emp1.departmentID = 37;strcpy(emp1.name, "John Smith");empPtr = &emp1; /* empPtr now points to emp1 */printf("employeeID is %i in department %i \n", empPtr->employeeID, empPtr->departmentID);emp2 = emp1;/* assigns values in ALL fields of emp1 to emp2 */

Page 35: 22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.

22&23-35

Rather than use the arrow operator we could use “ * ” the dereferencing operator. From the last example:printf("employeeID is %i in department %i \n",

(*empPtr).employeeID, (*empPtr).departmentID);However, do not write: printf("employeeID is %i in department %i \n",

*empPtr.employeeID, *empPtr.departmentID);

because in C this means (see operator precedence rules) printf("emplayeeID is %i in department %i \n",

*(empPtr.employeeID), *(empPtr.departmentID));

and these expressions are not equivalent. Why?

Therefore, it may be “safer” to use the arrow operator, so that we don’t make the mistake shown above.