How to use array of structure in C Linux Code.

How to enter details of 10 (multiple) employee ? Answer is array of employee structures.
#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[20];
//int age;
};

int main()
{
int i;
struct employee arr[3];
for(i=0;i<3;i++)
{
printf("Enter details of employess:");
scanf("%d%s",&arr[i],arr[i].name);
}
printf("Employee details:\n");
for(i=0;i<3;i++)
{
printf("id=%d, name=%s\n",arr[i].id, arr[i].name);
}
return 0;
}
/*******OUTPUT
Enter details of employess:1
Leal
Enter details of employess:2
Mitra
Enter details of employess:333
Jesus
Employee details:
id=1, name=Leal
id=2, name=Mitra
id=333, name=Jesus
*****************/
//Display structure details based on search conditions
//students's name and roll number should be stored in structure
//array of 10 student structure
//take input and display
//displaye the student detail whose roll number is entered
#include<stdio.h>
#define SIZE 2
struct student
{
char name[20];
int roll;
};
struct student arr[SIZE];//needs global
int i;
int rollSearch;

void add();
void disp();
void search();
int main()
{
//Array of 5 students
//struct student arr[5];//needs global
//enter student details
add();
//display students details
disp();
//display details of student whose roll number is entered
search();
return 0;
}
void add()
{
for(i=0;i<SIZE;i++)
{
printf("Enter name of student %d:",i+1);
scanf("%s",arr[i].name);
printf("Enter roll of student %d:",i+1);
scanf("%d",&arr[i].roll);
}
}
void disp()
{
printf("*************Student Details***************\n");
for(i=0;i<SIZE;i++)
{
printf("Name of student %d is:%s\n",i+1,arr[i].name);
printf("Roll of student %d is:%d\n",i+1,arr[i].roll);
}
}
void search()
{
int flag=0;
printf("*************Search Result***************\n");
printf("Enter Roll of whose details you want:");
scanf("%d",&rollSearch);
for(i=0;i<SIZE;i++)
{
if(arr[i].roll==rollSearch)//mind the syntax roll[i].roll
{
flag=1;
printf("Name of student %d is:%s\n",i+1,arr[i].name);
printf("Roll of student %d is:%d\n",i+1,arr[i].roll);
}
}
if(flag==0)
printf("Roll Number does not exist.\n");
}
/*Output:
Enter name of student 1:Leal
Enter roll of student 1:100
Enter name of student 2:Mitra
Enter roll of student 2:200
Enter name of student 3:Ram
Enter roll of student 3:300
Enter name of student 4:Raheem
Enter roll of student 4:400
Enter name of student 5:yeshu
Enter roll of student 5:500
*************Student Details***************
Name of student 1 is:Leal
Roll of student 1 is:100
Name of student 2 is:Mitra
Roll of student 2 is:200
Name of student 3 is:Ram
Roll of student 3 is:300
Name of student 4 is:Raheem
Roll of student 4 is:400
Name of student 5 is:yeshu
Roll of student 5 is:500
*************Search Result***************
Enter Roll of whose details you want:200
Name of student 2 is:Mitra
Roll of student 2 is:200
*/

How to access nested structure members in C C++ Linux

#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[10];
struct dob
{
int day;
int month;
int year;
}d;
};

int main()
{
struct employee e1;
//struct dob dd;//can decalre but cant use
e1.id=1000;
strcpy(e1.name,"LealMitra");
e1.d.day=10;
e1.d.month=7;
e1.d.year=2014;

printf("Employee details:\n");
printf("id=%d, name=%s, dob=%d-%d-%d \n",e1.id, e1.name, e1.d.day,e1.d.month,e1.d.year);

return 0;
}
/****************OUTPUT
Employee details:
id=1000, name=LealMitra, dob=10-7-2014
*****************/

How to Initialize structure member variables in C C++

Ways of initializing a structure in C and C++. How not to do mistake in initializing structures.
#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[20];
//Note that below ways of initializing structure is error.
//int id=10;//erro:because memory is not allocated for structure here.
//strcpy(name,"Ram");//error for same reason.
};

int main()
{
//one way of initializing structure.
struct employee e1={1000,"Leal"};//one way of initializing structure.
printf("Employee details:\n");
printf("id=%d, name=%s\n",e1.id,e1.name);


//Another way of initializing structure.
struct employee e2;
e2.id=1001;
strcpy(e2.name,"Mitra");
printf("\nEmployee details:\n");
printf("id=%d, name=%s\n",e2.id,e2.name);

//Yet another way of initializing  structure.
struct employee e3;
printf("Enter id :\n");
scanf("%d",&e3.id);
printf("Enter name :\n");
scanf("%s",&e3.name);
printf("\nEmployee details:\n");
printf("id=%d, name=%s\n",e3.id,e3.name);
return 0;
}
/******* OUTPUT
@ubuntu:~/headHunt$ gcc structureInitialization.c 
structureInitialization.c: In function ‘main’:
structureInitialization.c:30:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
@ubuntu:~/headHunt$ gcc structureInitialization.c 
structureInitialization.c: In function ‘main’:
structureInitialization.c:30:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
@ubuntu:~/headHunt$ ./a.out 
Employee details:
id=1000, name=Leal

Employee details:
id=1001, name=Mitra
Enter id :
200
Enter name :
blog

Employee details:
id=200, name=blog
@ubuntu:~/headHunt$ 
*****************/