9 Structures
9 Structures
9 Structures
1
Objectives
• Introduction to Structures
• Defining Structures
• Processing a structure
• User-defined Data types
• Passing structures to Functions
• Self-referential Structures
2
Introduction
Array is a data type to store multiple
values in the same data type.
Structure in another way is like array
but can store value from different
data type
An individual structure elements are
referred as member
3
Introduction
Array is a data type to store multiple
values in the same data type.
Structure in another way is like array
but can store value from different
data type
An individual structure elements are
referred as member
ID NO Name Address GPA
1123 John Li 121, Jalan Blue 3.78
4
How Structure look like?
Syntax
structure
tag
struct StructureTypeName
{
StructureMemberDeclarationsList
}; /*end struct*/
Example List of
declarations of its
struct faculty_struct
members
{
int faculty_idno;
char faculty_name;
int age;
char gender;
float GPA;
}; /*end faculty_struct*/
5
Declaring Structure Variables
After declaring a structure type, we may declare variables that
are of that type. A structure variable declaration requires these
elements:
1.The keyword struct
2.The structure type name
3.A list of variable names separated by commas
4.A concluding semicolon
struct tag {
datatype member1;
datatype member2;
datatype member3;
IMPORTANT:
}; Remember to put the “;”
at the end of structure
declaration
6
Structures
Itis possible to combine the declarations of a
structure type and a structure variable by
including the name of the variable at the end of
the structure type declaration.
struct faculty_struct {
int faculty_idno;
char faculty_name;
int age;
char gender;
float salary;
} faculty_record; /*end faculty_struct*/
struct cdCollection
{
char title[25];
char artist[20];
int numSongs;
float price;
char dataBought[9];
} cd1, cd2, cd3;
8
Nested Structures
Since member of structure can be of any datatype
Therefore structure can also contain structure as
its member
struct tag1
{
int member1;
struct tag2 variable;
};
Where structure tag2 had to be declared before
tag1.
9
Processing a structure
Members of a structure are usually processed
separately
Therefore need to access the structure
member individually
Each member of the structure can be
accessed by
variable.member
To access a member of a nested structure;
variable.member.submember
Where member is a structure type.
10
Example
based on the structure type faculty_struct [slide 5], and
the structure variable declaration
struct faculty_struct faculty_record;
faculty_record.faculty_idno = 1200;
strcpy(faculty_record.faculty_name, “Farid”);
faculty_record.age = 30;
faculty_record.gender = ‘M’;
faculty_record.salary = 7800.00;
11
Example printf("Here is the CD information: \n\n");
#include <stdio.h>
printf("Title: %s \n", cd1.title);
#include <stdlib.h>
printf("Artist: %s \n", cd1.artist);
#include <string.h>
printf("Songs: %d \n", cd1.numSongs);
printf("Price: %.2f \n",cd1.price);
int main()
printf("Date Bought: %s \n",
{ cd1.dateBought);
struct cdCollection
{ printf("\n");
char title[25]; system("PAUSE");
char artist[20]; return 0;
int numSongs; }
float price;
char dateBought[9];
} cd1;
12
Initializing Structure Variables
InC it is possible to initialize structure variables at
compile time. Compile time initialization of a
structure variable requires the following elements:
1.The keyword struct
2.The structure type name
3.The name of the structure variable
4.The assignment operator, =
5.A set of values for the members of the structure
variable, enclosed in braces, and separated by commas
6.A terminating semicolon
13
Example
struct faculty_struct
{
int faculty_idno;
char faculty_name;
int age;
char gender;
float salary;
} faculty_record = {1200, “Farid”, 30, ‘M’, 7800.00};
struct cdCollection
{
char title[25];
char artist[20];
int numSongs;
float price;
char dataBought[9];
} cd1={“Red Men”, “Sam and the sneeds”, 12, 11.95, “02/02/07”};
14
Initialization Rules
1. Theorder of values for structure variable
members enclosed in braces must match the
order of members in the structure type
declaration.
2. It
is possible to initialize only a subset of the
members of a structure variable at compile
time. However, that subset must always begin
with the first member and continue until your
target member without skipping any.
15
User-defined Data Types
(typedef)
User can defined their own data type by
typedef type new-type;
Example:
typedef int age;
Typedef also can be use in structures
where
typedef struct {…} new-type;
16
Structure and Pointers
Ifvariable represents a structure-type variable, then
&variable represent the starting address of that
variable
17
Example
customer.acct_no = &n;
#include <stdio.h> customer.acct_type = &t;
#include <stdlib.h> customer.name = "smith";
#include <string.h> customer.balance = &b;
struct printf("\n");
{ system("PAUSE");
int *acct_no; return 0;
char *acct_type; }
char *name;
double *balance;
}customer, *pc = &customer;
18
Operations on Structure Variables
19
Copying content between
structures
When you have two or more same
structure type, where
struct tag1{…} variable1, variable2;
Then
variable1 = variable2;
is a valid operation
20
Passing structure to Functions
There are several ways to pass structure-
type information to or from a function
Data can be pass o function by individual
member or the entire structures
Individual structure members treated as
ordinary single valued variable and can be
passed to a function as arguments in the
function call
21
Example of passing individual member
#include <stdio.h>
customer.balance =
#include <stdlib.h> adjust(customer.name, customer.acct_no,
#include <string.h> customer.balance);
adjust(&customer);
23
Multiple block data in structures
24
Structure array
int acctn;
#include <stdio.h> record *pt;
#include <stdlib.h>
#include <string.h> printf("\nAccount Record");
printf("\n--------------");
typedef struct{
char *name; for (acctn = 0; acctn <3; ++acctn)
int acct_no; {
char acct_type; pt = &customer[acctn];
double balance;
} record; printf("\n%s\t%d\t%c\t%.2f", pt->name, pt-
>acct_no, pt->acct_type, pt->balance);
}
int main()
{
printf("\n");
system("PAUSE");
static record customer[3] ={
return 0;
{"Smith",333,'C',33.33},
}
{"Jones",666,'C',66.66},
{"Brown",999,'C',99.99}
};
25
Self-referential structures
A structures that has a links (pointer) to
the next data that have same structure
Or a structure that have a member that
has the same structure as it owns.
Example
struct tag
{
datatype member1;
struct tag *ptvar;
}
struct list_element
{
char *item;
struct list_element *next;
} 26
Self-referentialstructures allow data to
linked together and provide flexible
operation and organization on the data.
The details of self-referential will be in
Data Structure and Algorithms (TMC1433).
27
Union
Defined as similar way as structure. The only
difference is that the union creates only one
memory space. The size of the memory
computed by the maximum size of its data
members.
Union student{
int id;
char name[10];
float gpa;
}u1;
28
Example
u1.id=100; 100
u1.gpa=3.75;
Printf(“%d %f”,u1.id,u1.gpa); Memory (10 bytes)
29
Example
u1.id=100; 3.75
u1.gpa=3.75;
printf(“%d %f”,u1.id,u1.gpa); Memory (10 bytes)
30
Example
u1.id=100; 3.75
u1.gpa=3.75;
printf(“%d %f”,u1.id,u1.gpa); Memory (10 bytes)
31
END
32