PART 9
PART 9
Basics of Structures:
Primary data types are used to hold a single value at a time. Arrays can be used to represent a group of
items that belongs to the same data type. If we want to represent different types of data items using a single
value (or) name then we can’t use an array.
Fortunately ‘C’ supports a constructed data type known as structure , which is a method for packing
data of different types. The concept of a structure is similar to that of a record in many other languages.
These files are called structure elements (or) members. Each member may belong to different types of
data types.
Structure Initialization:
Like any other data type, a structure variable can be initialized. But ‘C’ language doesn’t permit the
initialization of individual structure members with in the structures. The initialization must be done only in the
variable declaration.
Eg:
struct account
{
int act _ no;
char act _ type [20];
char cus _ name [20];
float balance;
}S1 = {1001, “savings”, “Kumar”, 10000.00};
void main ( )
{
struct account S2 = {1002 “current”, “Raju”, 15000.00};
}
Operator Meaning
S1 = S2 Assign S2 to S1.
S1==S2 Compare all members of S1 and S2 and return 1 if they are equal and
0 otherwise.
S1! = S2 Return 1 if all the members are not equal, 0 otherwise
Arrays of Structure:
We use structures to describe the format of a number of related variables. In some cases we need to
store the information belonging to number of structure variables. In such cases, we may declare an array of
structures where each element of the array representing a structure variable. For example,
Eg: struct marks;
{
int marks1;
int marks2;
int marks3;
}s [10];
By using above syntax we can store marks of 10 students.
We can also assign values at the time of declaration.
Eg: void main ( )
{
struct marks S[3] = {{30, 40, 25} {10, 20, 30,} {20, 30, 40}};
}
When we use Array of structures, while accessing a member we should specify the structure variable and its
subscript that is
S[0].marks1= 30;
S[0].marks2= 40;
S[0].marks3 = 25;
typedef:
‘C’ supports a feature known as “Type definition” that allows users to define an identifier that would
represent an existing data type. The user defined data type identifier can later be used to declare variables. It
takes the general form
typedef type identifier;
Where type refers to an existing data type and “identifier” refers to the “new” name given to the data type. The
existing data type may belong to any class type, including the user defined ones, but not the data type. typedef
can’t create new type.
batch1 and batch2 are declared as int variables and name1[50] and name2[50] are declared as floating point
array variables. The main advantage is that we can create meaningful data type names for increasing the
readability of the program.
enum:
Another user defined data type is enumerated data type provided by ANSI standard. It is defined as
follows
enum identifier {value1, value2,…………………..value n};
The “identifier” is a user defined enumerated data type which can be used to declare variables that can have
one of the values enclosed within the braces (known as enumeration constants). After this definition we can
declare variables to be this ‘new’ type as below.
enum identifier v1, v2,………….. vn.
The enumerated variable v1, v2, …………. vn can only have one of the values value1, value2, ……..valuen.
The assignments of the following types are valid.
V1= value3;
V5= value1;
The compiler automatically assigns integer digits beginning with 0 to all Enumeration constants. I.e., the
enumeration constant value1 is assigned 0, value2 is assigned 1 ……….
However, the automatic assignments can be over ridden by assigning values explicit to the enumeration
constants for example.
enum day { Monday = 1, Tuesday,Wednesday,Thursday,Friday,Sunday}.
Here, the constant Monday is assigned the value of 1. The remaining constants are assigned values that
increase successively by 1.
Bit Fields
Introduction
C allow integer members to be stored into memory spaces smaller than the compiler would ordinarily
allow.
These space-saving structure members are called bit fields, and their width in bits can be explicitly
declared.
Bit fields are used in programs that must force a data structure to correspond to a fixed hardware
representation and are unlikely to be portable.
The syntax for declaring a bit field is as follows:
type_specifier declarator :constant_expression;
A bit field declaration contains a type specifier followed by an optional declarator, a colon, a constant
integer expression that indicates the field width in bits, and a semicolon. A bit field declaration may not
use either of the type qualifiers, const or volatile.
The C99 implementation supports the following types.
int
In all implementations, the default integer type for a bit field is unsigned.
In C language, when we assign a value that is out of range to a bit field, the low-order bit pattern is
preserved and the appropriate bits are assigned.
Bit fields with a length of 0 must be unnamed. Unnamed bit fields cannot be referenced or initialized. A
zero-width bit field can cause the next field to be aligned on the next container boundary where the
container is the same size as the underlying type of the bit field.
Bit fields are also subject to the align compiler option. Each of the align suboptions gives a different
set of alignment properties to the bit fields.
The maximum bit field length is 64 bits. For portability, do not use bit fields greater than 32 bits in size.
The following restrictions apply to bit fields. We cannot:
Define an array of bit fields
Light 1 bit
Toaster 1 bit
Ac 4 bits
Clock 1 bit
Flag 1 bit
All references to structure fields must be fully qualified. For instance, we cannot reference the second field
by toaster. We must reference this field by kitchen.toaster.
The following expression sets the light field to 1:
kitchen.light = 1;
When we assign to a bit field a value that is out of its range, the bit pattern is preserved and the appropriate
bits are assigned. The following expression sets the toaster field of the kitchen structure to 0 because
only the least significant bit is assigned to the toaster field:
kitchen.toaster = 2;
Unions
These are a concept borrowed from structures and therefore follow the same syntax as that of structure.
However there is a major distinction between structure and a union in terms of storage. In structure each
member has its own storage location where as all the members of unions use the same location. This
implies that although a union may contain many members of different types, it can handle only one
member at a time.
Ex: Like structure a union can be declared using the key word union
union student
{
int rollno;
char name[20];
float avg;
};
In the above declaration union contains three variables each with a different data type. We can use only one of
them at a time. This is due to the fact that only one location is allocated for a union variable. The compiler
allocates a memory to a union variable that is large enough to hold the largest variable type in the union. In the
above declaration the member rollno requires 2bytes, member name requires 20bytes and the member avg
requires 4bytes. Hence in this case the union variable is allocated with 20bytes of memory.
Variable length argument is a feature that allows a function to receive any number of arguments. There are
situations where we want a function to handle variable number of arguments according to requirement.
Variable length arguments is an advanced concept in C language offered by c99 standard. In c89 standard,
fixed arguments only can be passed to the functions.
When a function gets number of arguments that changes at run time, we can go for variable length
arguments.
It is denoted as … (3 dots)
stdarg.h header file should be included to make use of variable length argument functions.