c structures with examples


Structures
Structure is a data type, which contains individual elements that can defer
in type.
Hence structure can contain integer, char, float, double, etc., elements.
Arrays, pointer and other structures can also be included in structures.
Individual elements are referred to as members.
Defining a Structure:
A structure being complicated than array should be defined in terms of its
individual members.
Syntax
struct <StructureName>
{
member 1;
member 2;
.
.
member n;
};
struct is a keyword, <name> is the name that identifies the structure of this
type.
member1, member2…etc; are individual member declarations.
The individual members can be ordinary variables, arrays, pointers or other
structures.
The member names within a structure should be distinct from one another.
A member name can be the same as the name of variable that is defined
outside the structure.
Once the composition of the structure has been defined individual structure
type variables should be declared as follows.
struct <name> var1, var2, …var n;
Processing a Structure:
The members of a structure are usually processed individually as separate
entities
structureVariable.Member
structureVariable refers to the name of the struct type variable.
Member refers to the name of the member within the structure.
Program
#include<stdio.h>
void main()
{
struct person
{
char name[20];
int day;
int month;
int year;
};
struct person p1;
char reply;
do
{
printf("\nEnter Name of The Person : ");
scanf("%s", p1.name);
printf("\nEnter The Date of Birth(DD-MM-YYYY) : ");
scanf("%d-%d-%d", &p1.day, &p1.month, &p1.year);
printf("\n\n");
printf("\nName of The Person : %s", p1.name);
printf("\n\nDate of Birth : %d-%d-%d", p1.day, p1.month, p1.year);
printf("\n\nWanna Continue [Y/N]? : ");
fflush(stdin);
reply = getchar();
} while (reply == 'Y' || reply == 'Y' );
}
Array of Structures:
Program
#include<stdio.h>
void main()
{
struct student
{
char name[20];
int sub1;
int sub2;
};
struct student s[10];
int k;
printf("\nEnter Student's Details : ");
for(k = 0; k < 10; k++)
{
printf("\nEnter %d Student Name : ", k+1);
scanf("%s", s[k].name);
printf("\nEnter Marks in Maths : ");
scanf("%d", &s[k].sub1);
printf("\nEnter Marks in Sciences : ");
scanf("%d", &s[k].sub2);
}
for(k = 0; k < 10; k++)
printf("\n\nStudent Name : %s \t Grand Total : %d", s[k].name,
s[k].sub1+s[k].sub2);
}
Passing Structures to Functions
Program
#include<stdio.h>
struct vehicle
{
int vno;
char model [10];
};
void main( )
{
void change(struct vehicle x);
struct vehicle modify(struct vehicle y);
struct vehicle v;
printf("\nEnter Vehicle Number : ");
scanf("%d", &v.vno);
printf("\nEnter Vehicle Model : ");
scanf("%s", v.model);
printf("\nVehicle Number is : %d", v.vno);
printf("\nVehicle Model is : %s", v.model);
printf("\nCalling Change Function...");
change(v);
printf("\nVehicle Number is : %d", v.vno);
printf("\nVehicle Model is : %s", v.model);
printf("\nCalling Modify Function...");
v = modify(v);
printf("\nVehicle Number is : %d", v.vno);
printf("\nVehicle Model is : %s", v.model);
}
void change(struct vehicle x)
{
printf("\n Vehicle Number is : %d", x.vno);
printf("\n Vehicle Model is : %s", x.model);
printf("\nEnter Model Name Again : ");
scanf("%s", x.model);
printf("\nThe Latest Entered Vehicle Model is : %s", x.model);
}
struct vehicle modify (struct vehicle y)
{
printf("\nVehicle Number is : %d", y.vno);
printf("\nVehicle Model is : %s", y.model);
printf("\nEnter Model Name Again : ");
scanf("%s", y.model);
printf("\nThe Latest Entered Vehicle Model is : %s", y.model);
return y;
}
Structures within a Structure:
Program
#include<stdio.h>
void main()
{
struct date
{
int d;
int m;
int y;
};
struct vehicle
{
int Vno;
char model[10];
struct date purch;
};
struct vehicle v;
printf("\nEnter Vehicle Number : ");
scanf("%d", &v.Vno);
printf("\nEnter The Vehicle Model : ");
fflush(stdin);
scanf("%s", v.model);
printf("\nEnter The Date of Purchase(DD-MM-YYYY) : ");
fflush(stdin);
scanf("%d-%d-%d", &v.purch.d, &v.purch.m, &v.purch.y);
printf("\nThe Entered Details for the Vehicle : ");
printf("\nThe Vehicle Number is : %d", v.Vno);
printf("\nThe Vehicle Model is : %s", v.model);
printf("\nThe Date of Purchase of the Vehicle is : %d-%d-%d", v.purch.d, v.purch.m,
v.purch.y);
}

Structure and Pointers:
Program
# include <stdio.h>
void main ()
{
struct vehicle
{
int vno;
char model[10];
};
struct vehicle v, *p;
p = &v;
printf("\nEnter Vehicle Number : ");
scanf("%d", p -> vno);
printf("\nEnter Vehicle Model : ");
scanf("%s", p -> model);
printf("\n\nThe Given Vehicle Number is : %d", p -> vno);
printf("\n\nThe Given Vehicle Model is : %s", p -> model);
}
Working with ‘typedef’ in C
It is used to create an alternative type for the original data type.
Syntax
typedef <originaltype> <new-type>;
Program
#include<stdio.h>
void main()
{
typedef char name[25];
typedef struct
{
int d; /* day */
int m; /* month */
int y; /* year */
} date;
name person;
date dob;
printf("\n\nEnter Your Name : "); scanf("%s", person);
printf("\nDate of Birth (DD-MM-YYYY) : ");
scanf("%d-%d-%d", &dob.d, &dob.m, &dob.y);
printf("\nThe Given Name is : %s\n\nDate of Birth : %d-%d-%d", person, dob.d,
dob.m, dob.y);
}

No comments: