Click here to Skip to main content
15,889,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
/*Write a program in c to store employee details in a structure named employee.
Store address of employee in another structure named address which is nested .
Then print the total information of the employee uing the concept of nested structure.*/
#include<stdio.h>
#include<stdlib.h>
struct address
{
	char adrs[30];
};
struct employee
{
	int empid;
	char name[20];
	char mobno[10];
	struct address ads;
};
int main()
{
	int n,i;
	struct employee *p;
	printf("The size of the array =>");
	scanf("%d",&n);
	p=(struct employee*)malloc(n*sizeof(struct employee));
	printf("\nThe Employee details :\n");
	for(i=0;i<n;i++)
	{
		printf("\n\nenter employee id\n\n");
		scanf("%d",&p[i].empid);
		printf("\nname\n");
		scanf("%s",p[i].name);
		printf("\nmobile number\n");="" scanf("%s",p[i].mobno);
		printf("\naddress\n");
		scanf("%s",p[i].ads.adrs);
	}
	free(p);
	printf("\nthe details are :");
	for(i="0;i<n;i++)
		printf("\n\nemployee printf("%d",p[i].empid);
	printf("\n\nname\n");
	printf("%s",p[i].name);
	printf("%s",p[i].mobno);
	printf("\n\naddress\n");
	printf("%s",p[i].ads.adrs);
	return 0;
}


What I have tried:

Allocating the memory dynamically.
Using structures.
Posted
Updated 8-Mar-20 21:36pm
v3
Comments
Stefan_Lang 9-Mar-20 3:37am    
I have removed those spurios ="" tokens from your code, and fixed the formatting.

Note that after fixing the formatting, the missing brace issue in the second for loop that Richard pointed out really stands out! Therefore: always make sure to properly indent your code.

1 solution

You have the following line after you have captured all the information
C++
free(p);

So you have just thrown away all the information that you carefully captured.

You are also missing an open brace after the second for statement. It should be
C++
for(i = 0; i < n; i++)
{
 
Share this answer
 
Comments
CPallini 9-Mar-20 6:42am    
5.
John R. Shaw 11-Mar-20 12:23pm    
And ending brace '}'. Also the weird '"' in the for loop will stop it from compiling.

Good catch on the 'free(p)'; otherwise BOOM!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900