Professional Documents
Culture Documents
General format:
struct type-name
{
data-type1 name1;
data-type2 name2;
------------- ---------
------------- ---------
};
Structures : Defining student structure 5
float cgpa;
};
Now Student data type behaves similar to built-in data types like
int, char etc.
Array vs. Structure 6
Array Structure
Collection of related data of Collection of different data
same type. types.
A derived data type from A programmer/user defined
existing data types(e.g int data type.
int[ ]) Need to define data type first,
Data type is often available, then declare variables/arrays of
just declare array the new type.
Declaring Structure variables 7
Assume the structure is defined as:
struct mytype
{
int n;
char c;
float f;
};
Declaring variable of type mytype.
struct mytype s1,s2;
Declaring structure variables-other way 8
struct mytype
{
int n;
char c;
float f;
}s1,s2;
2 variables of the same structure type can be copied the same way as
ordinary variables.
E.g. : if person1 and person2 belong to same structure, then:
person1=person2;
person2=person1;
C doesn’t permit logical operations on structure variables.
To compare structure variables , we do so by comparing members
individually.
16
Word Boundaries and Slack bytes 17
Computer stores structures using “word boundary”.
The size of word boundary is machine dependent.
In a computer with 2 bytes word boundary, the members of a structure are
stored left_aligned on the word boundary.
A character data takes one byte and an integer takes 2 bytes.one byte between
them is unoccupied. This unoccupied byte is known as slack byte.
Because of the slack byte values C does not support structure comparison
0123 1 2 3