The structure in C is a user-defined data type that can be used to group items of possibly different types into a single type. Basically, structures are a collection of values of different data types.
EXAMPLE : name (String)
For a student store the following :
roll no (Integer)
cgpa (Float)
Syntax :
struct student {
char name[100];
int roll;
float cgpa;
};
struct student s1;
s1.cgpa = 7.5;
Q1 Write a program to store the data of 3 students in C.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct student{
int roll;
float cgpa;
char name[100];
};
int main(){
struct student s1;
s1.roll=1423;
s1.cgpa=9.2;
// s1.name="Gagan";
strcpy(s1.name,"gagan");
printf("Student name=%s\n",s1.name);
printf("Student roll=%d\n",s1.roll);
printf("Student cgpa=%f\n",s1.cgpa);
struct student s2;
s2.roll=1343;
s2.cgpa=8.4;
// s1.name="Gagan";
strcpy(s2.name,"madavan");
printf("Student name=%s\n",s2.name);
printf("Student roll=%d\n",s2.roll);
printf("Student cgpa=%f\n",s2.cgpa);
struct student s3;
s3.roll=1343;
s3.cgpa=9.4;
// s1.name="Gagan";
strcpy(s3.name,"David");
printf("Student name=%s\n",s3.name);
printf("Student roll=%d\n",s3.roll);
printf("Student cgpa=%f\n",s3.cgpa);
return 0;
}
Benefits of using Structures
Saves us from creating too many variables
Good data management/organization
Array of Structures
Arrays can also be used in structures. Here's the syntax.
struct student COE[100];
struct student ECE[100];
struct student IT[100];
IT[0].roll = 200; ACCESS
IT[0].cgpa = 7.6;
Initializing Structures
struct student s1 = { "Ram", 1664, 7.9};
struct student s2 = { "Ragav", 1552, 8.3};
struct student s3 = { 0 };
Pointers to Structures
struct student s1;
struct student *ptr;
ptr =&s1;
Arrow Operator
(*ptr).code ptr->code
Passing Structure to function
Structures can be passed into functions either by reference or by value.
Ex :
struct student{
int roll;
float cgpa;
char name[100];
};
void printInfo(struct students1);
int main(){
struct student s1={134,4.4,"gagan"};
printInfo(s1);
return 0;
}
void printInfo(struct student s1){
printf("student information:\n");
printf("student.roll =%d\n",s1.roll);
printf("student.name =%d\n",s1.name);
printf("student.cgpa =%d\n",s1.cgpa);
}
Typedef Keyword
Typedef stands for type definition. typedef is simply a way of giving a new name to an existing data type. Basically its like creating alias or a nickname for data types.
Ex :
typedef struct computerengineeringstudent{
int roll;
float cgpa;
char name[100];
}coe;
int main(){
coe s1;
s1.roll=3434;
s1.cgpa=9.3;
strcpy(s1.name,"gagan");
printf("student name is%s\n",s1.name);
return 0;
}
Q2 Enter the address of 5 people it should include house no, city, state and block number in C.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct adress{
int houseno;
int block;
char city[100];
char state[100];
}adr;
void printadress(struct adress ads);
int main(){
struct adress ads[5];
printf("Enter infro for person 1 in format \nhouse no block city and state\n");
scanf("%d",&ads[0].houseno);
scanf("%d",&ads[0].block);
scanf("%s",ads[0].city);
scanf("%s",ads[0].state);
printf("Enter infro for person 2 in format \nhouse no block city and state\n");
scanf("%d",&ads[1].houseno);
scanf("%d",&ads[1].block);
scanf("%s",ads[1].city);
scanf("%s",ads[1].state);
printf("Enter infro for person 3 in format \nhouse no block city and state\n");
scanf("%d",&ads[2].houseno);
scanf("%d",&ads[2].block);
scanf("%s",ads[2].city);
scanf("%s",ads[2].state);
printf("Enter infro for person 4 in format \nhouse no block city and state\n");
scanf("%d",&ads[3].houseno);
scanf("%d",&ads[3].block);
scanf("%s",ads[3].city);
scanf("%s",ads[3].state);
printf("Enter infro for person 5 in format \nhouse no block city and state\n");
scanf("%d",&ads[4].houseno);
scanf("%d",&ads[4].block);
scanf("%s",ads[4].city);
scanf("%s",ads[4].state);
for (int i=0;i<5;i++){
printadress(ads[i]);}
}
void printadress(struct adress ads){
printf("adress of person is-\n %d,\t%d,\t%s,\t%s\n",ads.houseno,ads.block,ads.city,ads.state);
}
Q3 Create a structure to store vectors. Then make a function to return sum of 2 vectors in C.
struct vector{
int x;
int y;
};
void calcsum(struct vector v1,struct vector v2,struct vector sum);
int main(){
struct vector v1={3,6};
struct vector v2={3,5};
struct vector sum={0};
calcsum(v1,v2,sum);
return 0;
}
void calcsum(struct vector v1,struct vector v2,struct vector sum){
sum.x=v1.x+v2.x;
sum.y=v1.y+v2.y;
printf("sum of x is:%d\n",sum.x);
printf("sum of y is:%d\n",sum.y);
}
Q4 Create a structure to store complex numbers. (use arrow operator) in C.
struct complex{
int real;
int img;
};
int main(){
struct complex number={4,6};
struct complex *ptr=&number;
printf("real part=%d\n",ptr->real);
printf("img part=%d\n",ptr->img);
return 0;
}
Q6 Make a structure to store bank account information of a customer of ABC bank. Also, make an alias for it in C.
typedef struct bankaccount{
int acc;
char name[100];
}acc;
int main(){
acc acc1={123,"Gagan"};
acc acc2={124,"hagan"};
acc acc3={125,"iagan"};
printf("acc no=%d\n",acc1.acc);
printf("name=%s\n",acc1.name);
return 0;
}
Hope you enjoyed learning about Strings, this helps us to group items of possibly different types into a single type, hope this blog was useful. And don't forget to follow and share this blog with a friend who wants to learn about programming.
Thank you...