C Structures and Unions
C Structures and Unions
Why Structure?
struct struct_name
{
data_type identifier_list;
data_type identifier_list;
...
};
struct student
{
char name[80];
Example:
int roll_no;
float marks;
};
Note:
● When we define a structure, memory is not allocated until
we declare variable of struct_type
● A storage class cannot be assigned to an individual member
but we can assigned to entire structure.
Example:
struct point static struct point
{ {
static int a; int a;
int b; int b;
}; //error };
● Individual members cannot be initialized within a
structure-type declaration.
Example:
struct point struct point
{ {
int a=10;//error int a;
int b; int b;
};
};
Declaration of a variable of struct type
<struct-type> <identifier_list>;
Example:
1. struct taxcode
{
int number;
char letter;
};
struct taxcode taxcode1, taxcode_2;
//declares the structure type
2. struct student 3. struct personal
{ {
int roll_no; char name[20];
char name[10]; int age;
}; } my_record;
main( )
{ 4. struct
struct student stu1,stu2; {
………. char name[20];
………. int age;
} } my_record;
Limitations: we can’t create variable of this
structure type anywhere else in program.
Accessing members of a structure
A structure member can be accessed by using dot(.)
operator.
Syntax:
structure_variable . member_name
where structure_variable refers to-the name of a
structure-type variable, and member refers to the name
of a member within the structure.
● For example:
struct account
{
int acct_no;
char acct-type;
char name[80];
} customer;
customer is a structure variable of type account.
If we want to access the customer’s account number, we would
write
customer. acct_no ;
Structure Initialization
When we declare a structure, memory is not allocated for uninitialized
variable.
Way 1 : Declare and Initialize
struct student
{
char name[20];
int roll;
float marks;
}std1 = { "Pritesh", 67, 78.3 };
In the above code snippet, the structure is declared and as soon as
after declaration we have initialized the structure variable.
Way 2 : Declaring and Initializing Multiple Variables
struct student
{
char name[20];
int roll;
float marks;
}
std1 = {"Pritesh",67,78.3}, std2 = {“Ram",62,71.3};
} sub1={67};
There are three members of structure, only one is initialized , then
remaining two members are initialized with Zero.
Way 4 : Initializing inside main
struct student
{
int mark1;
int mark2;
int mark3;
};
void main()
{
struct student s1 = {89,54,65};
- - - - --
}
Way 5 : Initializing members inside main in any order
struct student
{
int mark1;
int mark2;
int mark3;
};
void main()
{
struct student s1 = {.mark2=10, .mark1=20};
printf(“%d %d”,s1.mark1,s1.mark2);
}
#include <stdio.h> printf("Enter marks: ");
struct student scanf("%f", &s.avg);
{ printf("Displaying Information:\n");
char name[50];
int roll; printf("Name: ");
float avg; puts(s.name);
} s; printf("Roll number: %d\n",s.roll);
int main() { printf("Marks: %f\n", s.avg);
printf("Enter information:\n"); return 0;
printf("Enter name: "); }
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
Size of an Structure
#include<stdio.h> OUTPUT:
struct book address of title=2686728
{ address of year=2686736
char title[5]; address of price=2686744
int year; size of structure=24
double price;
};
struct book b1 = {.title=“nitk", .year=20,.price=12.21};
void main()
{
printf("address of title=%u\n",b1.title);
printf("address of year=%u\n",&b1.year);
printf("address of price=%u\n",&b1.price);
printf("size of structure=%d\n",sizeof(b1));
}
Memory layout of structure:
Copy and Compare Structure Variables
● Two variables of the same structure type can be copied the same
way as ordinary variables.
● If person1 and person2 belong to the same structure, then the
following statements Are valid.
person1 = person2;
person2 = person1;
● In case, we need to compare them, we may do so by comparing
members individually.
person1.id==person2.id;
● The below statements are not permitted in C
person1 + person2
person1 = = person2
person1 ! = person2
person1 && person2
Program for copy and comparison of structure variable
struct class
if ( x==1)
{
int no; printf (“ \n student 2 & student 3 are same \n”);
char name [20]; else
float marks;
printf ( “\n student 2 and student 3 are different “);
};
main ( ) }
{
int x;
struct class stu1 = { 111, “Rao”, 72.50};
struct class stu2 = {222, “Reddy”, 67.80};
struct class stu3;
stu3 = stu2;
x = ( ( stu3.no= = stu2.no) && ( stu3.marks = = stu2.marks))?1:0;
Operations on structure variables in C
● There is a relatively small number of operations which C
directly supports on structures.
● A member with the dot operator along with its
structure variable can be treated like any other variable
name and can be manipulated using expressions and
operators.
Example: Complex number addition
#include<stdio.h>
void main()
Output:
{
2
struct complex
3
{ 4
float real; 5
float cmplex; 6+i8
} a, b, c;
scanf (“%f %f”, &a.real, &a.cmplex);
scanf (“%f %f”, &b.real, &b.cmplex);
c.real = a.real + b.real;
c.cmplex = a.cmplex + b.cmplex;
printf (“\n %f + i%f ”, c.real, c.cmplex); }
Example 2: Program to add two distances in inch-feet system using
structure
Enter information for 1st distance
Enter feet: 12
Enter inch: 12.2
distances = 25'-12.400000"
#include <stdio.h>
struct Distance sumOfDistances.feet = d1.feet+d2.feet;
{ sumOfDistances.inch = d1.inch+d2.inch;
int feet;
float inch; //If inch is greater than 12, changing it to feet.
} d1, d2, sumOfDistances; if (sumOfDistances.inch>=12.0)
int main() {
{ sumOfDistances.inch =sumOfDistances.inch-12.0;
printf("Enter information for 1st distance\n"); ++sumOfDistances.feet;
printf("Enter feet: "); }
scanf("%d", &d1.feet); printf("\n distances = %d\'-%f\"",
printf("Enter inch: "); sumOfDistances.feet, sumOfDistances.inch);
scanf("%f", &d1.inch); return 0;
}
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
Array of Structure
● Once a structure has been defined, we can declare an
array of structures
struct student
{
int roll_no ;
float marks ;
} st [3];
type.
Example: struct marks
{
int number;
float sub[2];
}st[2];
These elements can be accessed by using
st[0].sub[0]; st[1].sub[0];
st[0].sub[1]; st[1].sub[1];
struct Student
{ S.Total = 0;
int Roll; for(i=0;i<3;i++)
char Name[25]; {
int Marks[3]; printf("\n\nEnter Marks %d : ",i+1);
int Total; scanf("%d",&S.Marks[i]);
float Avg; S.Total = S.Total + S.Marks[i];
}; }
void main()
{ S.Avg = S.Total / 3;
int i;
struct Student S; printf("\nRoll : %d",S.Roll);
printf("\n\nEnter Student Roll : "); printf("\nName : %s",S.Name);
scanf("%d",&S.Roll); printf("\nTotal : %d",S.Total);
printf("\n\nEnter Student Name : "); printf("\nAverage : %f",S.Avg);
scanf("%s",&S.Name); }
Nested Structure
● Nested structure in C is nothing but structure within structure.
complex records.
● There are two methods to declare a structure within structure.
struct tag_name
{
Data_type name1: bit_length;
Data_type name2: bit_length;
……………………….
};
● Data type can either be int or unsigned int or signed int and the bit-length
number bits used for specified name.
● The bit-length is decided by the range of value to be stored.
● The largest value that can stored is 2^n-1, where n is the bit-length.
#include <stdio.h>