Professional Documents
Culture Documents
Table of Contents
1. Introduction 3
2. Defining a structure 4
2.1 Array vs. Structure 4
5. Structure Initialization 6
Rules for Initializing structure variables 6
7. Array of Structures 9
1. Introduction
❑ Arrays support packing the same type data together.
❑ Many times, packing of different types of data is necessary in programming.
❑ E.g. A Student record may contain a name which is a string, id an integer, cgpa a float, etc.
❑ How to store and process details of 60 students in a class?? (e.g: name,id,cgpa)
❑ One possible solution is:
char names[60][25];
int id[60];
float cgpa[60];
❑ This is not an elegant solution as processing of the data is tedious and code size will become large.
❑ Structures in C: Provides a better way to deal with such situations.
❑ Structures enable us to create new data types of our own..!!
❑ Such data types are known as ‘User Defined Data types’.
❑ Structures can combine different built-in data types to define new data types.
❑ Now Student data type behaves similar to built-in data types like int, char etc.
Size of a Structure type or structure variable
❑ Normally, Size of a structure is the total size of the members in that structure.
❑ For Student structure above, the size is 20 + 4 + 4 = 28 bytes, assuming character size 1 byte, size of int and
float are 4 bytes each.
❑ There are some exceptions to this. If size of structure is not a multiple of the word size** (in bytes) of the
system, some extra bytes will be added to the size to make it a proper multiple of the word size.
❑ E.g. For a 32-bit machine word size is 4 bytes(32 bits). If structure is as below:
struct Student
{
char name[25];
int id;
float cgpa;
};
We expect the size to be 25+4+4 = 33, but most of the systems add 3 more bytes to make it 36, a multiple of 4.
These additional 3 bytes will be unused.
** Word size of computer system refers to the size of the data processed by a computer's CPU in one go (these days,
typically 4 bytes(32 bit) or 8 bytes(64 bits).
❑ mytype is the new data type and s1,s2 are variables(a.k.a. instances) of that type!!
int main()
{
// accessing members & assigning values
s1.n = 10;
s1.c = 'a';
s1.f = 1.32;
return 0;
}
E.g.
struct mytype
{
int n;
char c;
float f;
};
void main()
{
struct mytype s1 = {10,'a',2.57};
printf("%d %c %f\n",s1.n,s1.c,s1.f);
}
Output:
10 a 2.570000
// Display again
printf("%s %d %f\n",s.name,s.id,s.cgpa);
}
Output:
Amit 1014 9.350000
Amit 1014 9.100000
struct Student
{
char name[20];
int rollno;
float cgpa;
};
int main()
{
struct Student s;
Output:
Enter student details: Amit 1024 9.44
Amit 1024 9.440000
Amit 1124 9.440000
int main()
{
struct Student s1 = {"Amit", 1234, 9.34};
struct Student s2;
Comparing structures:
Relation operators do not support comparing user-defined data types.
E.g.
struct Student students[10];
#include<stdio.h>
struct Student
{
char name[20];
int rollno;
float cgpa;
};
int main()
{
Output:
How many students?2
Enter student's data 1:
Amit 1202 9.6
Enter student's data 2:
Rohan 1022 8.5
Example:
struct mytype
{
int n;
char c;
float f;
}s1;
// Student Structure
// Creating variable along with structure definition
#include<stdio.h>
struct Student
{
char name[50];
int id;
float cgpa;
}s1,s[5];
int main()
{
// Variable declaration and initialization
printf("Enter student details(name,id,cgpa):");
scanf("%s %d %f", s1.name,&s1.id,&s1.cgpa);
printf("Name: %s \nID: %d \nCGPA: %.2f\n",s1.name,s1.id,s1.cgpa);
return 0;
}
Output:
Enter student details(name,id,cgpa):amit 2323 9.6
Name: amit
ID: 2323
CGPA:9.60
#include <stdio.h>
int main()
{
struct student s[10],temp;
int n,i,j;
printf("How many students?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d: Enter name name,rollno,cgpa:",i+1);
scanf("%s %d %f",s[i].name,&s[i].rollno, &s[i].cgpa);
}
{
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
return 0;
}
Sample I/O:
How many students?4
1: Enter name name,rollno,cgpa:
Rahul 100 8.65
2: Enter name name,rollno,cgpa:
Amit 101 7.84
3: Enter name name,rollno,cgpa:
Haris 105 9.43
4: Enter name name,rollno,cgpa:
Lana 110 8.12
Rank 2:
Rahul 100 8.65
Rank 3:
Lana 110 8.12
Rank 4:
Amit 101 7.84
Now, the combo variablename.member_sname is just like any other independent variable(not member of any
structure) in the program. It can be used in expressions.
E.g:
struct Student
{
char name[50];
int id;
int mark1;
int mark2;
};
struct Student s1 = {"Amit",100,50};
if(s1.id == 100)
s1. mark1+=10; // Using for arithmetic operation