Transcript Title

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.
• 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.
Structure Definition
struct name
{
variable declaration;
variable declaration;
.
.
};
the keyword struct announces that this is a structure
definition
Continue..
• struct hurricane
{
structure name/structure tag
char name[10];
int year, category;
Data members
};
• 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
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
name
h1 ?
struct hurricane h1;
year
?
?
category
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
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
Initializing Structures
• Using a declaration statements:
struct hurricane h1= {“Camille”,1969,5};
h1
“Camille”
1969
5
name
year
category
The declaration statement allocates memory
for the three data members, with their initial
values.
Another way to initialize..
• Using program statements:
h1.name = “Camille”;
h1.year = 1969;
h1.category = 5;
Initializing Structures
struct Rect
{
double x;
double y;
double width;
double height;
};
struct Rect r1 = {0,0,5,10};
Practice!
Write the C statements necessary to
output the value of the structure r1
defined in the previous slide.
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)
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;
}
Scope of Structure
• Member variables are local to the
structure.
• Member names are not known
outside the structure.
Assignment operator
Assignment operator is defined for
structure of the same type.
(Compiler looks at tag, not
composition.)
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;
}
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.
Arrays of Structures
Struct Car
{
char model [10];
};
• 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”;
Example
• struct hurricane
{
char name[10];
int year, category;
};
……
struct hurricane h[25];
h[0]
h[1]
……
h[24]
?
?
?
?
?
?
…
…
…
?
?
?
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;
Output
• To print the first array
– print_hurricane(h[0]);
– output:
Hurricane: Camille
Year: 1969, Category: 5
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.
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.
Call by Value Example
struct simple
{
int ival;
double dval;
};
int main(void)
{
void fun1(struct simple s);
//function prototype
struct simple s1 = {10, 1.5};
//structure variable
fun1(s1);
//call function fun1
printf(“%i %lf”, s1.ival , s1.dval );
return 0;
}
void fun1(struct simple s)
output?___________
{
s.ival++;
s.dval++;
}
To see the difference…
#include <stdio.h>
struct simple
{
int ival;
double dval;
};
int main(void)
{
void fun1(struct simple s);
//function prototype
struct simple s1 = {10, 1.5};
//structure variable
fun1(s1);
//call function fun1
printf("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 );
}
•
•
•
•
•
•
•
#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
•
•
•
}
•
•
•
•
double calcNet(struct employee temp) //funct header
{
return (temp.payrate * temp.hours);
}
printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay);
return 0;
Pointers to Structures
When using pointers to access structure members, the
arrow operator is used.
Example:
struct node
{
int data;
struct node *link;
};
struct node *nptr, n;
nptr = &n;
nptr->data = 2; //(*nptr).data
nptr->link = NULL;
Pointers to Structures
struct node
{
int data;
struct node *link;
};
int main(void)
{
struct node *nptr, n={2, NULL};
void fun1(struct node*);
nptr = &n;
fun1(nptr);
printf(“%i”, n.data );
return 0;
//declare structure variable
//function prototype
//pointer to structures
//call function fun1
}
Output?___________________
void fun1(struct node* n)
{
n->data++;
}
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;
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
medic=&engin;
(*medic).aa = 20;
medic->bb =15.7;
Using pointer to access the structure variable’s
members. Two methods are shown. Both accomplish
the same task.
printf("%d %lf\n",engin.aa,engin.bb);
}
Printing the engin member, using
pointer will modify the members of
engin
•
•
•
•
•
•
•
#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
•
•
•
}
•
•
•
•
•
double calcNet(struct employee *temp) //funct header
{
return (temp->payrate * temp->hours);
// or return (((*temp).payrate) * ((*temp).hours));
}
printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay);
return 0;