Skip to content

Commit 96fa957

Browse files
authored
Structures
1 parent f385894 commit 96fa957

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed

Array_of_Structures.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
typedef struct
5+
{
6+
int id;
7+
char name[20];
8+
float percentage;
9+
10+
}student;
11+
12+
void EnterDetails(student[],int); /*Function Declaration of the Function to Add Student Details*/
13+
int main()
14+
{
15+
student s[10]; /*Declaration of the Structure Variables*/
16+
int number, i;
17+
18+
printf("\nEnter the Number of Students :: ");
19+
scanf_s("%d", &number);
20+
21+
EnterDetails(s, number); /*Entering the Details of the Students through a Function (Call by Reference)*/
22+
23+
/*Displaying the Details of the Student through a For Loop*/
24+
printf("\nStudent Details are as follows :: ");
25+
for (i = 0; i < number; i++)
26+
{
27+
printf("\n************Student No %d :: **********",i+1);
28+
printf("\n ID :: %d", s[i].id);
29+
printf("\n Name :: %s", s[i].name);
30+
printf("\n Percentage :: %f", s[i].percentage);
31+
}
32+
33+
_getch();
34+
return 0;
35+
}
36+
37+
void EnterDetails(student s[], int n)
38+
{
39+
for (int i = 0; i < n; i++)
40+
{
41+
printf("\n**************Enter the Details for Student %d :: ***********",i+1); /*User Entering the Details into the Structure Variables*/
42+
printf("\nID/Roll No :: ");
43+
scanf_s("%d", &s[i].id);
44+
getchar(); /*To Consume the unwanted new line character*/
45+
printf("Name :: ");
46+
gets_s(s[i].name, 20);
47+
printf("Percentage :: ");
48+
scanf_s("%f", &s[i].percentage);
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
typedef struct
5+
{
6+
int id;
7+
char name[20];
8+
float percentage;
9+
10+
}student;
11+
void DisplayDetails(student);
12+
int main()
13+
{
14+
student s1, s2; /*Declaration of the Structure Variables*/
15+
int x;
16+
17+
s1 = { 111,"Rao",72.50 }; /*Initializing the Values of the Structure Variables*/
18+
s2 = { 222,"Reddy",67.00 };
19+
20+
student s3;
21+
s3 = s2; /*Equating one Structure Variable to the other*/
22+
23+
DisplayDetails(s1); /*Displaying the Details of the Student through a Funtion*/
24+
x = ((s2.id == s3.id) && (s2.percentage == s3.percentage) && (strcmp(s2.name, s3.name) == 0)) ? 1 : 0;
25+
if (x)
26+
{
27+
printf("\nStudent 3 is a copy of Student 2 i.e. Both are the same.");
28+
DisplayDetails(s2);
29+
}
30+
else
31+
{
32+
printf("\nStudent 3 and Student 2 different entries.");
33+
DisplayDetails(s2);
34+
DisplayDetails(s3);
35+
}
36+
37+
_getch();
38+
return 0;
39+
}
40+
41+
void DisplayDetails(student s)
42+
{
43+
printf("\nStudent Details are as follows :: ");
44+
printf("\n ID :: %d", s.id);
45+
printf("\n Name :: %s", s.name);
46+
printf("\n Percentage :: %f", s.percentage);
47+
}

Define_Access_Structure_Members.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
typedef struct student
5+
{
6+
int id;
7+
char name[20];
8+
float percentage;
9+
10+
}student;
11+
void DisplayDetails(student); /*Function declaration*/
12+
int main()
13+
{
14+
student s1, s2; /*Declaration of the Structure Variables*/
15+
student s3 = { 3, "Devon Smith", 98 };
16+
17+
s1.id = 1;
18+
strcpy_s(s1.name, "John Smith");
19+
s1.percentage = 62.5;
20+
21+
printf("\nEnter the Details for Student 2 ::"); /*User Entering the Details into the Structure Variables*/
22+
printf("\nID/Roll No :: ");
23+
scanf_s("%d", &s2.id);
24+
getchar(); /*To Consume the unwanted new line character*/
25+
printf("\nName :: ");
26+
gets_s(s2.name, 20);
27+
printf("\nPercentage :: ");
28+
scanf_s("%f", &s2.percentage);
29+
30+
DisplayDetails(s1); /*Displaying the Details of the Student through a Funtion*/
31+
32+
printf("\nStudent Details are as follows :: ");
33+
printf("\n ID :: %d", s2.id);
34+
printf("\n Name :: %s", s2.name);
35+
printf("\n Percentage :: %f", s2.percentage);
36+
37+
DisplayDetails(s3);
38+
39+
_getch();
40+
return 0;
41+
}
42+
43+
void DisplayDetails(student s)
44+
{
45+
printf("\nStudent Details are as follows :: ");
46+
printf("\n ID :: %d", s.id);
47+
printf("\n Name :: %s", s.name);
48+
printf("\n Percentage :: %f", s.percentage);
49+
}

Structure_Union_Pointer.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
typedef struct
5+
{
6+
int id;
7+
char name[20];
8+
float percentage;
9+
10+
}student;
11+
typedef union {
12+
int id;
13+
char name[20];
14+
float percentage;
15+
}str;
16+
void DisplayDetails(student *);
17+
int main()
18+
{
19+
student s1, s2, *s4; /*Declaration of the Structure Variables*/
20+
21+
s4 = &s2;
22+
s1.id = 1;
23+
strcpy_s(s1.name, "John Smith");
24+
s1.percentage = 62.5;
25+
26+
printf("\nEnter the Details for Student 2 ::"); /*User Entering the Details into the Structure Variables*/
27+
printf("\nID/Roll No :: ");
28+
scanf_s("%d", &s4->id); /*&s2.id*/
29+
getchar(); /*To Consume the unwanted new line character*/
30+
printf("Name :: ");
31+
gets_s(s4->name, 20); /*s2.name*/
32+
printf("Percentage :: ");
33+
scanf_s("%f", &s4->percentage);
34+
35+
DisplayDetails(&s1); /*Displaying the Details of the Student through a Funtion*/
36+
37+
printf("\nStudent Details are as follows :: ");
38+
printf("\n ID :: %d", s2.id);
39+
printf("\n Name :: %s", s2.name);
40+
printf("\n Percentage :: %f", s2.percentage);
41+
42+
str u1, u2;
43+
u1.id = 5;
44+
strcpy_s(u1.name, "John Edgar");
45+
u1.percentage = 78;
46+
printf("\n\n\n************* Union Details are as follows :: ***************");
47+
printf("\n ID :: %d", u1.id);
48+
printf("\n Name :: %s", u1.name);
49+
printf("\n Percentage :: %f", u1.percentage);
50+
printf("\n*******************************************");
51+
printf("\nSize of the Struture Variable is %d", sizeof(s1));
52+
printf("\nSize of the Union Variable is %d", sizeof(u1));
53+
54+
_getch();
55+
return 0;
56+
}
57+
58+
void DisplayDetails(student *s)
59+
{
60+
printf("\nStudent Details are as follows :: ");
61+
printf("\n ID :: %d", s->id);
62+
printf("\n Name :: %s", s->name);
63+
printf("\n Percentage :: %f", (*s).percentage);
64+
}

0 commit comments

Comments
 (0)