Вы находитесь на странице: 1из 61

C Structures and Unions

Why Structure?

● With array, we can only declare one data type per


array.
● For different data type, we need another array
declaration.
● It is single type aggregate data type.
● Structure overcomes this problem by declaring
composite data types which can consist different
types.
For example :
● You want to store some information about a person: his/her name,
citizenship number and salary.
● You can easily create different variables name, citNo, salary to
store these information separately.
● In future you would want to store information about multiple
persons.
● Now, you'd need to create different variables for each information
per person: name1, citNo1, salary1, name2, citNo2, salary2.
● A better approach will be to have a collection of all
related information under a single name Person, and use
it for every person.

● This collection of all related information under a single


name Person is a structure.

“Structure is a collection of variables of different types


under a single name”.
DIFFERENCE BETWEEN C VARIABLE, C ARRAY AND C
STRUCTURE:

● A normal C variable can hold only one data of one data


type at a time.
● An array can hold group of data of same data type.
● A structure can hold group of data of different data types
and Data types can be int, char, float, double and long
double etc.
Defining a 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};

In this example, we have declared two structure variables. After


declaration of variable we have initialized two variable.
Way 3 : Initializing Single member
struct student
{ Data Type Default value if not
initialized
int mark1;
integer 0
int mark2; float 0.00
int mark3; char \0

} 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

Enter information for 2nd 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];

● Individual members can be accessed as:


st[0].roll_no
st[1].roll_no
st[2].roll_no
Program to store and access “id, name and percentage” for 3 students.
#include<stdio.h>
for(i = 0; i < MAX; i++ )
#include<string.h>
{
#define MAX 10
printf("\nEnter details of student %d\n\n", i+1);
struct student
printf("Enter name: ");
{
scanf("%s", arr_student[i].name);
char name[20];
printf("Enter roll no: ");
int roll_no;
scanf("%d", &arr_student[i].roll_no);
float marks;
printf("Enter marks: ");
};
scanf("%f", &arr_student[i].marks);
}
int main()
printf("\n");
{
printf("Name\tRoll no\tMarks\n");
struct student arr_student[MAX];
for(i = 0; i < MAX; i++ )
int i;
{
printf("%s\t%d\t%.2f\n", arr_student[i].name,
arr_student[i].roll_no, arr_student[i].marks);
} }
Array within Structure
● We have used arrays of characters inside a structure. Same way we
can use single or multi-dimensional arrays of type int or float .
● In an array of structures, each element of an array is of the structure

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.

● The declaration is same as the declaration of data type in structure.


● Structure within structure (or) nesting of structure is used to create

complex records.
● There are two methods to declare a structure within structure.

● Programmers can use either one method to declare structure


within structure.
○ Embedded Structure Declaration

○ Two Separate Structure Declaration


Way 1 : Declare Embedded structures
struct Employee
{
char ename[20]; Accessing Nested Members :
int ssn; Accessing Month Field : emp1.DATE.month
float salary; Accessing day Field : emp1.DATE.day
Accessing year Field : emp1.DATE.year
struct date
{
int date;
int month;
int year;
}DATE;
}emp1={“Rahul”,454,120000,{ 12,2,2000}};
Example:
int main( )
#include <stdio.h> {
#include <string.h> //storing employee information
struct Employee e1.id=101;
{ strcpy(e1.name, “Vinay");
int id; e1.doj.dd=10;
e1.doj.mm=11;
char name[20];
e1.doj.yyyy=2014;
struct Date //printing first employee information
{ printf( "employee id : %d\n", e1.id);
int dd; printf( "employee name : %s\n", e1.name);
int mm; printf( "employee date of joining (dd/mm/yyyy)
int yyyy; %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
}doj; return 0;
}
}e1;
Way 2 : Declare two separate structures
struct date
{ Accessing Nested Elements :
int date; 1. Structure members are accessed using dot
int month; operator.
int year; 2. ‘date‘ structure is nested within Employee
}; Structure.
3. Members of the ‘date‘ can be accessed using
struct Employee ’employee’
{ 4. emp1 & DATE are two structure names
char ename[20]; (Variables)
int ssn; Explanation Of Nested Structure :
float salary; Accessing Month Field : emp1.DATE.month
struct date DATE; Accessing day Field : emp1.DATE.day
Accessing year Field : emp1.DATE.year
}emp1;
Example: program illustrating nested structure
#include <stdio.h>
#include <string.h>
struct college int main()
{ {
int college_id; struct college stu_data = {1, "Raju", 90.5,{71145,
char college_name[50]; "NITK"}};
}; printf(" Id is: %d \n", stu_data.id);
struct student printf(" Name is: %s \n", stu_data.name);
{ printf(" Percentage is: %f \n\n",stu_data.percentage);
int id; printf(" College Id is: %d
char name[20]; ",stu_data.clg_data.college_id);
float percentage; printf(" College Name is: %s
// structure within structure \n",stu_data.clg_data.college_name);
struct college clg_data; return 0;
}stu_data; }
Structures and Functions
● Structures can be passed as an argument to a function and can be
used by the functions for performing operations on them.
● They are 3 methods by which the values of the structure can be
transferred from one function to another.
Method 1:
● The first method is to pass each member of a structure as actual
argument of the function call.
● The actual arguments are then treated independently as ordinary
variables.
#include<stdio.h>
void display(char ch[], int a, int n)
void main()
{
{ printf("\n Name of student :%s", ch);
struct stud printf("\n Age of Student :%d", a);
{ printf("\n Roll Number :%d", n);
char name[20]; }
int age, no;
}s;
void display(char [], int, int); //function prototype should be below to the
structure declaration otherwise compiler shows error
printf("\n Enter name of student :");
gets(s.name);
printf("\n Enter Age and roll Num:");
scanf("%d%d", &s.age, &s.no);
display(s.name, s.age, s.no);
}
Method 2: Pass by value
● In this method the entire structure is passed as an argument to a
function.
● Since only a copy of the structure is sent, any changes made to
structure members are not reflected the original structure.
● It is therefore necessary for the function to return the entire
structure back to the calling function.
General format of sending a copy of the structure is as follows
function_name(Structure_variable_name);
#include<stdio.h> display(e);
struct emp printf("\n Employee name :%s", e.name);
{ printf("\n Employee id :%d", e.id);
char name[20];
printf("\n Employee salary:%f", e.sal);
int id;
float sal; }
}; void display(struct emp e )
void main( ) {
{ strcpy(e.name,"rahul");
void display(struct emp) ; printf("\n Employee name :%s", e.name);
struct emp e;
printf("\n Employee id :%d", e.id);
printf(“\n Enter name”);
e.sal=e.sal+1000;
gets(e.name);
printf(“\n Enter ID “); printf("\n Employee salary:%f",e.sal);
scanf(“%d”,&e.id); }
printf(“Enter salary :”);
scanf(“%f”,&e.sal);
Method 3: Pass by Reference
● Employs concept of pointers.
● The address of the structure is passed to the function. Any
changes made to the structure members are reflected on to
the original structure.
Type-Defined structures
● The C programming language provides a keyword called typedef,
which you can use to give a new name to an existing type.
● Following is an example to define a term BYTE for one-byte
numbers −
typedef unsigned int BYTE;

● After this type definition, the identifier BYTE can be used as an


abbreviation for the type unsigned int.
For example:
BYTE b1, b2;
● Example:
#include<stdio.h>
typedef int INT;
main()
{
INT a=10,b=20;
printf("%d", fun(a,b));
}
fun(INT x, INT y)
{
return x+y;
}
● We can use the keyword typedef to define a structure as follows
#include <stdio.h>
#include <string.h> printf( "Book title : %s\n", b1.title);
typedef struct Books printf( "Book author : %s\n", b1.author);
{ printf( "Book subject : %s\n", b1.subject);
char title[50]; printf( "Book book_id : %d\n", b1.book_id);
char author[50]; return 0;
char subject[100]; }
int book_id;
} Book;
int main( ) {
Book b1;
strcpy( b1.title, "C Programming");
strcpy( b1.author, "Dennies Ritchee");
strcpy( b1.subject, "C Programming fundamentals");
b1.book_id = 6495407;
C UNION
● Unions are mainly used to conserve memory. Unions are a special form of
Structures.
● Unions also follow the same syntax as structures.
● The major distinction between them is in the terms of storage.
● In structures, each member has its own storage location, whereas all the
members of a union use the same location.
● Though unions may contain many members of different types, it can
handle only one member at a time.
● Unions are useful in applications involving multiple members where values
are not assigned to all the members at any one time.
Unions can be declared using the keyword union as follows
union item
{
int m;
float x;
char c;
}code;
● Declares a variable code of type union .
● The union contains three members, each with a different data
type. However, 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, irrespective of size.
● The compiler allocates a piece of storage that is large enough to
hold the largest variable type in the union.
● In the declaration above the member x requires 4 bytes which is
the largest among the members.
● To access a union member, use the same syntax that we can use
for structure members. That is,
code.m
code.x
code.c
Accessing of union members
● During accessing, we should make sure that we are accessing the
member whose value is currently stored.
For example, the statements such as
code.m = 65;
code.x = 6651.38;
printf("%d".code.m);
Note: The above would produce erroneous output
(which is machine dependent).
Example:
#include<stdio.h>
union book
{
char title[7];
int year; OUTPUT:
float price; title=Truth
}; year=1094933545
union book b1 = {“Truth", 20,12.21}; price=7373728
void main() size of union=8
{
printf(" title=%s\n",b1.title);
printf(" year=%d\n",b1.year);
printf("price=%f\n",b1.price);
printf("size of union=%d\n",sizeof(b1));
}
printf("Union record1 values example\n");
Example: printf(" Name : %s \n", record1.name);
#include <stdio.h> printf(" Subject : %s \n", record1.subject);
#include <string.h> printf(" Percentage : %f \n\n",
union student record1.percentage);
{ // assigning values to record2 union variable
printf("Union record2 values example\n");
char name[20];
strcpy(record2.name, "Manu");
char subject[20]; printf(" Name : %s \n", record2.name);
float percentage; strcpy(record2.subject, "Physics");
}; printf(" Subject : %s \n", record2.subject);
int main() record2.percentage = 99.50;
{ union student record1; printf(" Percentage : %f \n", record2.percentage);
return 0;
union student record2;
}
// assigning values to record1 union variable
strcpy(record1.name, "Ram");
strcpy(record1.subject, "Maths");
record1.percentage = 86.50;
BIT FIELDS
● Integer occupies 4 bytes of memory which is equal to 32bits. Most
of the time, we don’t use all the 32 bits to store the integer. So
there is a wastage of memory.
● To serve this purpose, C supports the concept of sharing data items
packed in a word of memory.
● A bit field is a set of adjacent bits whose size can be from 1 to 31
bits in length.
The general form of bit fields definition is:

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>

// A space optimized representation of date


struct date
{
// d has value between 1 and 31 int main()
unsigned int d: 5; {
printf("Size of date is %d bytes\n",
// m has value between 1 and 31 sizeof(struct date));
unsigned int m: 4; struct date dt = {31, 12, 2014};
unsigned int y; printf("Date is %d/%d/%d", dt.d, dt.m, dt.y);
}; return 0;
}
NOTE:

1) We cannot have pointers to bit field members .

2) It is implementation defined to assign an out-of-range

value to a bit field member.

3)Array of bit fields is not allowed.


Example 1:
// File name - structure.c //file name- structure.c
#include <stdio.h> struct student
#include <string.h> {
#include "structure.c" /* header file where C int id;
structure is declared */ char name[20];
int main() float percentage;
} record;
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
#include <stdio.h>
#include <string.h>
struct struct_example
{
int integer;
float decimal;
char name[10];
};
union union_example
{
int integer;
float decimal;
char name[10];
};
void main()
{ struct struct_example s={18,38,“good"};
union union_example u={18,38,“good"};
printf("structure data:\n integer: %d\n“ "decimal: %.2f\n name: %s\n“, s.integer, s.decimal,
s.name);
printf("\nunion data:\n integeer: %d\n decimal: %.2f\n name: %s\n", u.integer, u.decimal, u.name);
printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));
printf("\n Accessing all members at a time:");
s.integer = 183, s.decimal = 90;
strcpy(s.name, “good”);
printf("structure data:\n integer: %d\n decimal: %.2f\n name: %s\n”, s.integer,
s.decimal, s.name);
u.integer = 18, u.decimal = 90;
strcpy(u.name, "good”);
printf("\nunion data:\n integeer: %d\n decimal: %.2f\n name: %s\n”, u.integer,
u.decimal, u.name);

printf("\n Accessing one member at time:");


printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
strcpy(s.name, "C programming");
printf("\nname: %s\n", s.name);
printf("\n union data:");
u.integer = 240;
printf("\ninteger: %d", u.integer);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
strcpy(u.name, "C programming");
printf("\nname: %s\n", u.name);

printf("\nAltering a member value:\n");


s.integer = 1218;
printf("structure data:\n integer: %d\n decimal: %.2f\n name: %s\n“, s.integer, s.decimal, s.name);
u.integer = 1218;
printf("union data:\n integer: %d\n decimal: %.2f\n name: %s\n”, u.integer, u.decimal, u.name);
}
Accessing one member at time:
Output:
Structure data:
Structure data:
Integer:240
Integer:18
Decimal:128.000000
Decimal:38.000000
Name :C programming
Name :good
Union Data
Union Data
Integer:240
Integer:18
Decimal:128.000000
Decimal:45656454546
Name :C programming
Name :46565746
Altering a member value:
Size of structure:20
Structure data
Size of union:12
Integer:1218
Accessing all members at a time
Decimal:128.000000
Structure: Integer:183
Name :C programming
Decimal:90.0000000
Union data
Name :good
Integer:1218
Union: Integer:64656665645
Decimal:0.000000
Decimal:4536353463453645.65645
Name :gdffdffdd
Name :good
● Difference between structure and union

Вам также может понравиться