Structures Flashcards

1
Q

structures

A

collection of values of different datatypes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

two classification of datatypes

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Array vs Structures benefits

A

Arrays saves us from defining the same datatype again and again .
Structures saves us from defining different variables and good data management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Array of structures

A

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};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initializing structures

A

struct student s1={“shradha”,1664,7.9};
struct student s2={“amru”,34,8.9};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pointers to structure

A

struct student s1;
struct student *ptr;
ptr=&s1;

printf(“name is :%s”,(*ptr).name);

*ptr points to the whole datatypes structure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Arrow operator

A

(*ptr).name —– ptr->name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

passing structure to function

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

typedef keyword

A

used to create alias name for datatypes

typedef struct engineering{
int roll;
float cgpa;
}eng;

now instead of struct engineering we can use eng;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

create a structure to store address of 5 people

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

function to calculate sum of vectors

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

printing complex number using arrows

A

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);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly