Вы находитесь на странице: 1из 3

for writing details of a car:-

car1-name,prize,color.
car2-name,prize,color.
similarly for 100 cars we have to do the same
you must thing for a array but it is of same datatype.

so heres comes the intro structure and union


struct
{
char name;//global scope
int number;
}car1,car2;
int main()
{
car1.name="bmw";//data member
car1.number="2";
car2.name="bmw";
car2.number="2";
}
for local scope:-
struct {
char *name;
int age;
int salary;
}e1,e2;
int manager(}
{
struct {//local scope we have to write all again
char *name;
int age;
int salary;
}manager;

manager.age=27;
if manager.agr>30
manager.salary=55k;
else
mamager.salalry=66k
return manager.salary;
}
int main()
{
printf("%d",enter thensllary of e1 nd e2);
scanf("%d",&e1.salary,&e2.salary);
printf("%d",manager());
return 0;
}

#structure tag :-

struct employee{
char *name;
int age;
int salary;
}e1,e2;
int manager//function
{struct employee manager;
do;
}
int main()//function;

#typedef

it gives freedom to user to create its own type.


typedef struct car{

}car;
int main()
{
struct car c1;
we can write it now as
car c1;
}
#initailizing and accessing the members of structure/////

we can access member using dot operator

#array of strucutre//////
struct car{char *name;
int age;
int salary;
};
int main()
{
struct car c[2];
int i;
for(i=0;i<=2;i++)
{
printf("enter the car %d",i+1);
scanf("%d",&c[i].name);
printf("enter the car %d",i+1);
scanf("%d",&c[i].age);
printf("enter the car %d",i+1);
scanf("%d",&c[i].salary);
}
printf("\n");
for(i=0;i<=2;i++)
{
printf //
print statment;;;;;;;---
}
######pointer to strcuture::::
struct abc{
int x;
int y;
};
int main()
{
struct abc a={0,1};
struct abc *ptr=&a;
printf("%d %d",ptr->x,ptr->y);//////////////////////ptr-> is equivalent to(*ptr).x
return 0;
}
######how memory is allocated to structure members---------

----when an object of some structure type is declared then some contiguous block of
memory will be allocated to strcuture members........
struct abc {
char a;
char b;
int d;
}
var;
//////////////////////size of var/////6 byte
structure padding :----- it is used to save the no of cycles of processor by using
structure padding.......
total size of var :---8bytes....

#####unions######
----union is a user defined datatype but unlike structures union members share same
memory loaction...........
eg:-
struct abc{
int a;
char b;
}vr;////having same different memory allocation
while in union same memory alloaction....

memory allocation :-----


1.static memory allocation......
-memory allocated at the compile time
-it is fixed .
-wastage of size
-enetred the data more than given than program may crash
2.dynamic memory......
memory allocated at the run time....//////////////////////////////////////heap is
the palce where the dynamic memory allocation occured.///////////////

pointers are the way to access the DMA...


malloc
calloc
realloc
free
syntax:--- malloc -------(void*)malloc(size_t size)

int *pte =(int *)malloc(n*sizeof(int));

Вам также может понравиться