Structure
Structure
int x = 9;
• In programming we need something for holding data, and variable is the way
to do that.
• A data type in a programming language is a set of data with
predefined values. Examples of data types are: integer, float, string,…
• There are two kind of data types in programming languages:
• System-defined data types (also called Primitive data types)
• User-defined data types
System-defined data types
• The data types are defined by languages
• The number of bits are allocated to each data type is fully depend on
the language and compiler
• For example: in C language:
• int: 2 bytes (actual value depends on compiler)
• float: 4 bytes
•…
User-defined data types
• The user-defined data types are defined by the user himself.
• In C we can create structure. In C++/Java, we can create class.
• For example:
struct student{
char[20] name;
int id;
};
struct student x;
Data structure
• Data structure is a particular way of storing and organizing data in a
computer so that it can be used efficiently.
• General data structure: Array, Linked List, Queue, Stack,…
• Data structure is divided into two types:
• Linear data structure
• Non-linear data structure
Type Definition
• Syntax: typedef type Name ;
• Name becomes a name you can use for the type
• Examples: typedef struct {
typedef struct student Student; char[20] name;
Student x; /* x is an struct student */ int id;
Student* PtrToStudent } Student;
typedef char *STRING;
STRING sarray[10];
/* sarray is an array of char *’s, equivalent to declaring:
char *sarray[10]; */
Structured Variables
• Group of related values (but unlike array, where the values are not necessarily of
the same type)
• Each part of structure is a field, with an associated value
• The group is treated as a single unit (a single variable with parts)
VariableName
Field1 Field2 Field3 Field4
Structured Variable
d1.month = 12;
d1 d1.day = 2;
d1.year = 1970;
month day year
}
12 2 1970
Structure Initialization
• Can initialize structured variable by giving value for each field (in the
order fields are declared)
• Syntax:
STYPE Svar = { FVal1, FVal2, FVal3, … };
• Example:
typedef struct { d1
int month, day, year;
month day year
} DATE;
12 2 1970
DATE d1 = { 12, 2, 1970 };
Structure Assignment
• Can assign value of one structured var to another
• variables must be of same type (same name)
• values are copied one at a time from field to corresponding field
• Example:
d1
typedef struct {
int month, day, year; month day year
} DATE; 12 2 1970
2060
2061
2062
2063
2064
2065
2066
2067
Structure Example
#include <stdio.h>
printf(”Enter:\n”);
typedef struct { printf(" ID#: ");
int id; scanf("%d",&s1.id);
float gpa; printf(" GPA: ");
scanf("%f",&s1.gpa);
char class;
printf(" Class: ");
} Student;
scanf(" %c",&s1.class);
void main() {
Student s1; printf(”S#%d (%c) gpa =
%.3f\n",s1.id,s1.class,s1
s1 .gpa);
id gpa class }
Passing Structures as Parameters
• A field of a structure may be passed as a parameter (of the type of
that field)
• An advantage of structures is that the group of values may be passed
as a single structure parameter (rather than passing individual vars)
• Structures can be used as
• value parameter: fields copied (as in assignment stmt)
• reference parameter: address of structure passed
• return value (resulting structure used in statement) -- not all versions of C
allow structured return value
• best to use type-defined structures
Structure as Value Parameter
#include <stdio.h> void printS(Student s) {
/* Struc. Param. named s
typedef struct { created, fields of arg
int id; (s1) copied to s */
printf("S#%d (%c) gpa =
float gpa; %.3f\n",s.id,s.class,s.gpa);
char class; }
MyType V;
Nested Structure (cont)
Fields of V:
Alternate mechanism (preferred):
V.A /* int field */
typedef struct {
V.D /* structure field */
char B;
V.D.B /* char field */
float C;
V.D.C /* float field */
} MyDType;
typedef struct {
int A;
V
MyDType D;
} MyType; D
A
B C
MyType V;
Initializing Nested Structures
• To initialize a nested structure we give as the value of the structure a
list of values for the substructure:
StrucType V = { Field1Val, Field2Val, Field3Val, … }
where FieldXVal is an item of the form
{ SubField1Val, SubField2Val, SubField3Val, … }
if Field X is a structured field
• Previous example (MyType)
MyType V = { 43, { ‘A’, 3.5 } }; V
D
A
B C
43
A 3.5
Representing Table Data
• For many programs it is appropriate to keep track of a table of
information where we know several things about each entry in the
table:
Name ID Class GPA
Rich 42 F 2.00
Cathy 38 O 3.96
Keith 2 J 3.95
Karen 1 S 4.00
• We would like to keep the values for each entry in the table together
as one unit
Table: Array of Structures
• One mechanism for representing a table of information is to use an array where
each member of the array is a structure representing the info about a line of the
table:
students
int option; }