30
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures

Engineering Problem Solving with C Fundamental Concepts

  • Upload
    aricin

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

Engineering Problem Solving with C Fundamental Concepts. Chapter 7 Structures. Structure Basics. A structure is a collection of data values, called data members , that form a single unit. But the individual parts of the data do not have to be the same type . - PowerPoint PPT Presentation

Citation preview

Page 1: Engineering Problem Solving with C  Fundamental Concepts

Engineering Problem Solving with C

Fundamental Concepts

Chapter 7

Structures

Page 2: Engineering Problem Solving with C  Fundamental Concepts

Structure Basics• A structure is a collection of data values, called

data members, that form a single unit. • But the individual parts of the data do not have

to be the same type.• Structures are often called aggregate data type,

because they allow multiple data values to be collected into a single data type.

• Data members are individual data values within a structure.

• A structure provides a means of grouping variables under a single name for easier handling and identification.

• Unlike arrays, the data members can be of different types.

Page 3: Engineering Problem Solving with C  Fundamental Concepts

Structure Definition

struct name{

variable declaration;variable declaration;..

};

the keyword struct announces that this is a structure definition

Page 4: Engineering Problem Solving with C  Fundamental Concepts

Continue..

• struct hurricane structure name/structure tag{

char name[10];int year, category;

};

• After a structure has been defined, structure variables can be defined using declaration statements.

• Members can be declared to be of any valid C data type the tag node may now be used just like predefined types

int, char, etc

Data members

Page 5: Engineering Problem Solving with C  Fundamental Concepts

Declaring Structure Variables

• The statements to define structure appear before the main function

• They are often stored in the header file.

• Define a variable of this structure by the following statement

struct hurricane h1; ???

h1 nameyear

category

Page 6: Engineering Problem Solving with C  Fundamental Concepts

Example

struct node{

int data;node *link;

};

• the above defines a structure type • the name of the structure type (node) is called the structure tag• the identifiers declared inside the braces are called member

names. Members can be declared to be of any valid C data type

• the tag node may now be used just like predefined types int, char, etc

Page 7: Engineering Problem Solving with C  Fundamental Concepts

Declaring Structure Variables

struct node n1;struct node *head;

The structure n1 is a collection of smaller variables called member variables. The member variables are accessed using the dot (.) operator;

head =&n1;n1.data = 2;n1.link = NULL;

2

Page 8: Engineering Problem Solving with C  Fundamental Concepts

Initializing Structures

• Using a declaration statements:

struct hurricane h1= {“Camille”,1969,5};

“Camille”1969

5

h1 nameyear

category

The declaration statement allocates memory for the three data members, with their initial values.

Page 9: Engineering Problem Solving with C  Fundamental Concepts

Another way to initialize..• Using program statements:

h1.name = “Camille”;h1.year = 1969;h1.category = 5;

Page 10: Engineering Problem Solving with C  Fundamental Concepts

Initializing Structures

struct Rect{

double x;double y;double width;double height;

};struct Rect r1 = {0,0,5,10};

Page 11: Engineering Problem Solving with C  Fundamental Concepts

Practice!

Write the C statements necessary to output the value of the structure r1 defined in the previous slide.

Page 12: Engineering Problem Solving with C  Fundamental Concepts

Input and Output• We can use scanf and fscanf statements

to read values into the data members of a structure.

• printf and fprintf to print the values.• The structure member operator must be

used to specify an individual data member• Example:

– printf(“Hurricane: %s”,h1.name);– printf(“Year: %d, Category:

%d”,h1.year,h1.category)

Page 13: Engineering Problem Solving with C  Fundamental Concepts

Example:code#include <stdio.h>#include <string.h>

/* Define structure to represent a hurricane */struct hurricane{ char name[10]; int year, category;};

int main (void){/* Declare variables */ struct hurricane h1;

/* Initialization */ //struct hurricane h1 = {"Camille",1969,5}; another way to initialize strcpy(h1.name,"Camille"); //h1.name = "Camille"; h1.year = 1969; h1.category = 5;

printf("%s, %d, %d \n", h1.name, h1.year, h1.category); return 0; }

Page 14: Engineering Problem Solving with C  Fundamental Concepts

Scope of Structure

• Member variables are local to the structure.

• Member names are not known outside the structure.

Page 15: Engineering Problem Solving with C  Fundamental Concepts

Assignment operator

Assignment operator is defined for structure of the same type. (Compiler looks at tag, not composition.)

Page 16: Engineering Problem Solving with C  Fundamental Concepts

Example#include <stdio.h>#include <string.h>

struct Car{

char model[10], color[10];double price;int year;

};int main(){struct Car Car1, Car2;

Car1.year = 99;Car1.price = 18599.99;strcpy(Car1.color,"red");strcpy(Car1.model,"Contour");

/* Copy all data from Car1 to Car2. */Car2 = Car1;

/* print the data*/printf("%d, %lf, %s, %s \n", Car1.year,Car1.price, Car1.color, Car1.model);

printf("%d, %lf, %s, %s \n", Car2.year,Car2.price, Car2.color, Car2.model);

return 0;}

Page 17: Engineering Problem Solving with C  Fundamental Concepts

Arrays of Structures

• One benefit of having structure type in C is that we can create arrays of structures.

• With that, we can solve problems much more quickly and easily.

Page 18: Engineering Problem Solving with C  Fundamental Concepts

Arrays of Structures

• Arrays of structures may be declared in the same way as other C data types.

struct Car Ford[20];

• Ford[0] references first structure of Ford array.

Ford[0].model = “Taurus”;

Struct Car

{

char model [10];

};

Page 19: Engineering Problem Solving with C  Fundamental Concepts

Example

• struct hurricane

{

char name[10];

int year, category;

};

……

struct hurricane h[25];

? ? ?

? ? ?

… … …

? ? ?

h[0]

h[24]

……

h[1]

Page 20: Engineering Problem Solving with C  Fundamental Concepts

Continue..

• To access an individual data members : specify the array name, a subscript, and the data member name.

• Example:h[0].name = “Camille”;h[0].year = 1969;h[0].category = 5;

Page 21: Engineering Problem Solving with C  Fundamental Concepts

Output

• To print the first array– print_hurricane(h[0]);

– output:

Hurricane: Camille

Year: 1969, Category: 5

Page 22: Engineering Problem Solving with C  Fundamental Concepts

Structures as Arguments to Functions

• Entire structures can be passed as arguments to functions.

• When a structure is passed as an argument to a function, it is a call-by-value reference. When a function referenced is made, each data member of the actual parameter is passed to the function and is used as the value of the corresponding data member of the formal parameters– Changes made to the formal parameters do not

change the argument.

Page 23: Engineering Problem Solving with C  Fundamental Concepts

Continue

• A pointer to a structure may also be passed as an argument to a function.– Changes made to the formal parameters

also change the argument.

Page 24: Engineering Problem Solving with C  Fundamental Concepts

Call by Value Examplestruct simple{

int ival;double dval;

};

int main(void){

void fun1(struct simple s); //function prototypestruct simple s1 = {10, 1.5}; //structure variablefun1(s1); //call function fun1printf(“%i %lf”, s1.ival , s1.dval );return 0;

}void fun1(struct simple s) output?___________{

s.ival++;s.dval++;

}

Page 25: Engineering Problem Solving with C  Fundamental Concepts

To see the difference…#include <stdio.h>

struct simple{

int ival;double dval;

};

int main(void){

void fun1(struct simple s); //function prototypestruct simple s1 = {10, 1.5}; //structure variablefun1(s1); //call function fun1printf("in the main program:\n");printf("%i %lf\n", s1.ival , s1.dval );return 0;

}void fun1(struct simple s){

s.ival++;s.dval++;printf("within the function:\n");printf("%i %lf\n",s.ival , s.dval );

}

Page 26: Engineering Problem Solving with C  Fundamental Concepts

• #include <stdio.h>• struct employee //define structure name employee• {• int idnum;• double payrate;• double hours;• };

• double calcNet(struct employee); //funct prototype• • int main()• {• struct employee emp={6782,8.93,40.5};• double netPay;• netPay=calcNet(emp); //pass copy of the values in variable emp of structure employee

• printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay);• return 0;• }

• double calcNet(struct employee temp) //funct header• {• return (temp.payrate * temp.hours);• }

Page 27: Engineering Problem Solving with C  Fundamental Concepts

Pointers to Structures

When using pointers to access structure members, thearrow operator is used.

Example: struct node{

int data;struct node *link;

};struct node *nptr, n;nptr = &n;nptr->data = 2; //(*nptr).datanptr->link = NULL;

Page 28: Engineering Problem Solving with C  Fundamental Concepts

Pointers to Structuresstruct node{

int data;struct node *link;

};

int main(void){

struct node *nptr, n={2, NULL}; //declare structure variablevoid fun1(struct node*); //function prototype

nptr = &n; //pointer to structuresfun1(nptr); //call function fun1printf(“%i”, n.data );return 0;

}Output?___________________

void fun1(struct node* n){

n->data++; }

Page 29: Engineering Problem Solving with C  Fundamental Concepts

Source Code#include <stdio.h>

struct data{

int aa;double bb;

};void main(void){

struct data engin;struct data *medic;

engin.aa=8;engin.bb=12.5;

medic=&engin;

(*medic).aa = 20;medic->bb =15.7;

printf("%d %lf\n",engin.aa,engin.bb);}

Structure Definition

Declaring a pointer to structure ( a variable that can store the address of the beginning of a data structure)

Assigning an address to the pointer

Using pointer to access the structure variable’s members. Two methods are shown. Both accomplish the same task.

Printing the engin member, using pointer will modify the members of engin

Page 30: Engineering Problem Solving with C  Fundamental Concepts

• #include <stdio.h>• struct employee //define structure name employee• {• int idnum;• double payrate;• double hours;• };

• double calcNet(struct employee *temp); //funct prototype• • int main()• {• struct employee emp={6782,8.93,40.5};• double netPay;• netPay=calcNet(&emp); //pass address of emp of structure employee

• printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay);• return 0;• }

• double calcNet(struct employee *temp) //funct header• {• return (temp->payrate * temp->hours); • // or return (((*temp).payrate) * ((*temp).hours));• }