Understanding C Structures and Variables
Understanding C Structures and Variables
Structure:-
A Structure is a collection of logically related elements, possibly of different types, having a single
name.
Definition of a Structure:-
A structure definition forms a template that may be used to crate structure objects. The variables
that make up the structure are called members of the structure.
Tagged Structure
Structure Variables
Typedef Structure
1) Tagged Structure:-
The structure definition associated with the structure name is referred as tagged structure.
It does not create an instance of a structure and does not allocate any memory.
The general form or syntax of tagged structure definition is as follows:-
struct tag_name
{
type1 member1;
type2 member2;
……………
……………
};
struct is the keyword which tells the compiler that a structure is being defined.
Employee is an identifier representing the structure name (tag_name).
Eid, Ename and age are members of a structure and are themselves are not variables. They do not
occupy any memory.
Note:
Memory is not reserved for the structure definition since no variables are associated with
the structure definition. The members of the structure do not occupy any memory until they
are associated with the structure variables.
How to Declare a Structure variable:-
The structure can be declared in the following way:
The above statement will create a structure variable for the structure employee.
Once a structure variable is created memory is allocated for this structure variable.
Now a variable of type struct Employee is created which is of derived type, the memory is
allocated for the variable EMP.
In the structure Employee there are 3 structure members
1) Eid
2) Ename
3) Eage
What is the size of Structure variable?
Structure size depends upon the members of that structure.
The size for the structure variable is sum of sizes of the individual members.
In our example the size of structure variable EMP is 2+20+2 = 24 bytes.
Because, the members of structure Employee are int, char int types. So, sum of individual
members size = 2 bytes+20 bytes+ 2 bytes (Sum =24 bytes).
Note: - Structure definition appears at the beginning of the program file, before any variables or
functions are defined.
2) structure variables:-
struct tag_name
{
type1 member1;
type2 member2;
……………
} var1, var2…;
Example:
Employee details:-
struct Employee
{
int Eid;
char Ename[20];
int Eage;
}EMP1,EMP2,EMP3;
Here EMP1,EMP2,EMP3 are three structure variables which represents three employees.
3) Type-defined structure:-
The structure definition associated with keyword typedef is called type-defined structure.
This is the most powerful way of defining the structure.
The syntax of type- defined structure is
typedef struct
{
type1 member1;
type2 member2;
……
} TYPE_ID;
Note: - by using typedef it is not possible to declare a variable. But, we can have
user defined data type. TYPE_ID will be treated as the new data type.
INITIALIZATION OF STRUCTURES:-
struct Employee
{
int Eid;
ACCESSING STRUCTURES:-
.
structure_variable_name structure_member_name
Before dot, there must always be a structure variable. After the dot, there must always be a
structure element.
#include <stdio.h>
struct Employee
{
int Eid;
char Ename[20];
int Eage;
};
int main()
{
struct EMP1,EMP2;
printf(“\n\t Enter Eid for Emp1:”);
scanf(“%d”,&[Link]);
printf(“\n\t Enter Ename for Emp1:”);
scanf(“%s”,[Link]);
printf(“\n\t Enter Eage for Emp1:”);
scanf(“%d”,&[Link]);
printf(“\n\t EMP1 Eid : %d”, [Link]);
printf(“\n\t EMP1 Ename : %s”, [Link]);
printf(“\n\t EMP1 Eage : %d”, [Link]);
printf(“\n\t Enter Eid for Emp2:”);
scanf(“%d”,&[Link]);
printf(“\n\t Enter Ename for Emp2:”);
scanf(“%s”,[Link]);
printf(“\n\t Enter Eage for Emp2:”);
#include <stdio.h>
struct Student
{
char Sname[7];
char Sregno[11];
char Sbranch[4];
}s1,s2;
int main()
{
clrscr();
fflush(stdin);
printf("\n\t Enter student s1 Name:");
gets([Link]);
printf("\n\t Enter student s1 registered Number:");
gets([Link]);
printf("\n\t Enter student s1 Branch:");
gets([Link]);
printf("\n\t Enter student s2 Name:");
gets([Link]);
printf("\n\t Enter student s2 registered Number:");
gets([Link]);
printf("\n\t Enter student s2 Branch:");
gets([Link]);
#include <stdio.h>
typedef struct
{
char state_Name[15];
char state_bird[10];
char state_CM[3];
} States;
void main()
{
getch();
}
OPERATIONS ON STRUCTURES:-
1) Copying of structure variables or members:-
Copying of two structure variables is achieved using assignment operator. But, one
structure variable can be assigned to another structure variable of the same type.
Example:-
struct Student
{
char Sname[7];
char Sregno[11];
char Sbranch[4];
}s1,s2;
s2 = s1;
By writing the above statement entire structure s1 will be copied into structure s2.
We can also copy one structure members into another structure members in the
following way:
[Link] = [Link];
By writing the above statement the member‟s sname of s1 structure gets copied into
sname of s2 structure.
Note: - Structures or structure members can be copied only if they are of same type
otherwise copying is not possible.
2) Comparing to structures:-
If we nest one structure in another structure such structure is known as Nested Structure. (Or)
A structure which includes another structure is called a Nested Structure i.e., structure can be
used as a member of another structure.
The following is the syntax for nesting a structure:-
struct tag_name1
{
type1 member1;
…….
…….
struct tag_name2
{
type1 member1;
……
……
} var;
};
If we have to initialize the members of inner structure, we have to use the following
syntax:-
OuterStructure_variable_name .Innerstructure_variable_name.innerstructure_member_name
= value;
Outerstructure_variable_name .Outerstructure_member_name;
Syntax to access the members of outer structure members is as follows:-
Outerstructure_variable_name.Innerstructure_variable_name.innerstructure_member_name;
In this example we use a nested structure which holds the data of a cricket player.
#include <stdio.h>
#include <string.h>
struct Playerinfo
char pname[30];
char country[20];
struct Trackrecord
int total_matches;
int runs_scored;
float strike_rate;
float Avg;
}tr;
int age;
};
void main()
struct Playerinfo p;
clrscr();
strcpy([Link],"Suresh Raina");
strcpy([Link],"India");
[Link] = 28;
[Link].total_matches = 211;
[Link].runs_scored = 5206;
[Link].strike_rate = 93.38;
[Link] = 35.41;
puts([Link]);
puts([Link]);
printf("\n\n\t\t ");
getch();
O/P :-
To access the values of the members of the structure using pointers we need to perform following
things,
If ptr is a pointer to a structure, then the structure itself can be accessed using indirection
operator as shown below,
Once the structure pointer is initialized, then each member of the structure can be
accessed using dot operator as shown below,
(*ptr).structure_member1
(*ptr).structure_member2
struct student
{
int rno;
char name[10];
float avg;
};
Note: The parenthesis to all three expressions is necessary. We should not omit the parenthesis.
For example,
ptr
4001
5001
#include <stdio.h>
struct TrainRes
{
char P_name[30];
int age;
int train_no;
char coach[5];
int seat_no;
int breath_no;
char doj[25];
char source[30];
char destiny[40];
int fair;
};
int main()
{
struct TrainRes Tr;
struct TrainRes *Trs;
Trs = &Tr;
clrscr();
printf("Enter The details:\n");
printf("\n Enter P_name:\n");
gets(Trs->P_name);
printf("Enter age:\n");
scanf("%d",&Trs->age);
printf("Enter Train Number:\n");
scanf("%d",&Trs->train_no);
fflush(stdin);
printf("Enter Coach No:\n");
gets(Trs->coach);
printf("Enter seat Number:\n");
scanf("%d",&Trs->seat_no);
printf("Enter breath Number:\n");
scanf("%d",&Trs->breath_no);
fflush(stdin);
printf("Enter date of Journy:");
gets(Trs->doj);
printf("Enter Journy From:\n");
gets(Trs->source);
printf("Enter Journy To:\n");
gets(Trs->destiny);
fflush(stdin);
printf("Fair for Journy:\n");
scanf("%d",&Trs->fair);
printf("Displaying Details of passenger reservation:\n");
printf("****************************************");
printf("\n\tP_name:\t");
puts(Trs->P_name);
printf("\n\tage:\t %d",Trs->age);
age: 32
Train Number: 7542
Coach No: S9
Seat_no: 9
breath_no: 18
date of Journy: 23/12/2015
Destiny: Canada
Fair : 6000
EXAMPLE:-
#include <stdio.h>
#include <conio.h>
struct Employee
{
char ename[30];
float sal;
char location[30];
};
void Empdata_display(char [], char []);
void main()
{
struct Employee e;
clrscr();
printf("Enter Employee name:\n");
gets([Link]);
printf("Enter employee sal:\n");
scanf("%f",&[Link]);
fflush(stdin);
printf("Enter Employee Location:\n");
gets([Link]);
Empdata_display([Link],[Link]);
getch();
}
In the above example the Empdata_display () is a called function which will display
the details of employee which are stored in structure.
We will call this function by passing individual members of structure. In the below
statement we pass 2 members of structure individually.
Empdata_display([Link],[Link])
ename and location are two members of a structure that we had passed to function.
The function will display the name of employee and his location by taking structure
members as parameters.
The general format of sending a copy of a structure to the called function is:
The called function must be declared for its type, appropriate to the data type it is
expected to return.
The structure variable used as the actual argument and the corresponding formal
argument in the called function must of the same struct type.
The return statement is necessary only when the function is returning some data back
to the calling function. The expression may be any simple variable or structure
variable or an expression using simple variables.
Bit field:-
A bit field is a set of adjacent bits whose size can be from 1 to 16 bits length. A
word can therefore be divided in to a number of bit fields. The name and size of bit
fields are defined using structure. Since bit fields are defined within a structure, the
various bits can be accessed in a way we access individual members of a structure.
struct tag_Name
{
Unsigned int name1: bit_length;
Unsigned int name2: bit_length;
…
…
Unsigned int namen: bit_length;
};
Remember that a signed bit field should have at least 2bits (1bit for sign). The bit_length is
decided by the range of value stored. The largest value that can be stored is 2 n-1, where n is
bit_length.
The internal representation of bit fields is machine dependent. Some machines store bits from
left to right and others from right to left. The below Figure illustrates the layout of bit fields,
assuming a 16-bit word that is ordered from right to left.
The first field always starts with first bit of the word.
A bit can not overlap integer boundaries. This is, the sum of lengths of all fields in a
structure should not be more than the size of a word. In case, it is more, the overlapping
field is automatically forced to the beginning of the next word.
There can be unnamed field declared with size. Example,
unsigned : bit_length; Such fields provide within a word.