Structures Flashcards
structures
collection of values of different datatypes
two classification of datatypes
inbuilt datatypes
user defined datatypes
user defined datatypes can include say 4bytes of mem for int in which you can store character too
struct student
{
int roll;
float cgpa;
char name[100];
};
int main() {
struct student s1;
s1.roll=20919008;
s1.cgpa=8.45;
strcpy(s1.name,”Amrithesh k”);
printf("roll no is %d\n",s1.roll); printf("name is %s\n",s1.name); }
here for each user defined datatype a size of 100+4+4=108 bytes is reserved.
struct student is the datatype for this structure (like int for integer)
Array vs Structures benefits
Arrays saves us from defining the same datatype again and again .
Structures saves us from defining different variables and good data management.
Array of structures
struct student ECE[100];
struct student EEE[100];
EEE[0].roll=123
EEE[0].cgpa=234
struct student
{
int roll;
float cgpa;
char name[100];
};
int main() {
struct student EEE[10];
EEE[0].roll=1;
strcpy(EEE[0].name,”Harry”);
EEE[0].cgpa=9.4;
printf("name of student 1 is :%s",EEE[0].name); }
int main(){
struct book library[5];
library[0]=(struct book){“a”,”b”,45};
library[1]=(struct book){“a”,”b”,10};
library[2]=(struct book){“a”,”b”,50};
library[3]=(struct book){“a”,”b”,20};
library[4]=(struct book){“a”,”b”,15};
Initializing structures
struct student s1={“shradha”,1664,7.9};
struct student s2={“amru”,34,8.9};
Pointers to structure
struct student s1;
struct student *ptr;
ptr=&s1;
printf(“name is :%s”,(*ptr).name);
*ptr points to the whole datatypes structure
Arrow operator
(*ptr).name —– ptr->name
passing structure to function
include <stdio.h></stdio.h>
void printinfo(struct student s1);
printinfo(s1);
#include <string.h>
#include <ctype.h></ctype.h></string.h>
struct student
{
int roll;
float cgpa;
char name[100];
};
void printinfo(struct student x);
int main() {
struct student s1={1994,9.45,”harry”};
printinfo(s1);
}
void printinfo(struct student x){
printf(“name :%s\n”,x.name);
printf(“roll no:%d\n”,x.roll);
printf(“cgpa :%f\n”,x.cgpa);
}
this is an example for call by value
typedef keyword
used to create alias name for datatypes
typedef struct engineering{
int roll;
float cgpa;
}eng;
now instead of struct engineering we can use eng;
create a structure to store address of 5 people
include <stdio.h></stdio.h>
#include <string.h>
#include <ctype.h></ctype.h></string.h>
struct address{
int houseno;
char city[100];
char state[100];
};
int main(){
struct address adds[5];
printf(“enter info for person 1 :\n”);
scanf(“%d”,&adds[0].houseno);
scanf(“%s”,&adds[0].city);
scanf(“%s”,&adds[0].state);
printf(“enter info for person 2 :\n”);
scanf(“%d”,&adds[1].houseno);
scanf(“%s”,&adds[1].city);
scanf(“%s”,&adds[1].state);
printaddress(adds[0]); printaddress(adds[1]); }
void printaddress(struct address add){
printf(“address is %d %s %s\n”,add.houseno,add.city,add.state);
}
function to calculate sum of vectors
include <stdio.h></stdio.h>
#include <string.h>
#include <ctype.h></ctype.h></string.h>
struct vector{
int x;
int y;
};
void calcsum(struct vector v1,struct vector v2,struct vector sum);
int main(){
struct vector v1={3,5};
struct vector v2={4,6};
struct vector sum={0};
calcsum(v1,v2,sum);
}
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 vector is \n %d x + %d y”,sum.x,sum.y);
}
printing complex number using arrows
include <stdio.h></stdio.h>
#include <string.h>
#include <ctype.h></ctype.h></string.h>
struct complex{
int real;
int img;
};
int main(){
struct complex c1={23,34};
struct complex *ptr=&c1;
printf("real part is : %d\n",ptr->real); printf("img part is : %d\n",ptr->img);
}