Click here to Skip to main content
15,885,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
//pgm using array of structures just input and print the array of structs
#include<stdio.h>
	struct emp
	{
	int eid;
	int sid;
	char* ename;
	};

main()
{
struct emp e[2];
int i,j;
		for(i=0;i<=2;i++)

		{
		e[i].eid=i+1;

		printf("enter name of employee %d\t",i+1);
		scanf("%s",&e[i].ename);
		printf("enter supervisor id of employee %d \n",i+1);
		scanf("%d",&e[i].sid);
		}

			for(j=0;j<=2;j++)
			{
			printf("%d,%s,%d \n",e[i].eid,e[i].ename,e[i].sid);
			}
}






output

enter name of employee 1	jith
enter supervisor id of employee 1 
101
enter name of employee 2	jeev
enter supervisor id of employee 2 
102
enter name of employee 3	jay
enter supervisor id of employee 3 
103
0,����w,0 
0,����w,0 
0,����w,0
Posted
Updated 10-Sep-14 3:10am
v2
Comments
[no name] 10-Sep-14 7:34am    
Why are you using 'j' in your for loop but printing your array using 'i'?
jithu ajith 10-Sep-14 7:43am    
sry that was a mistake during debugging;
i rectified it now core dump occured

new
output


enter name of employee 1 jith
enter supervisor id of employee 1
8
enter name of employee 2 jeev
enter supervisor id of employee 2
9
enter name of employee 3 jay
enter supervisor id of employee 3
11
Segmentation fault (core dumped)
[bats@localhost c]$

Quote:
struct emp e[2];
Here you are declaring an array having two items, namely e[0] and e[1].



While here
Quote:
for(i=0;i<=2;i++){/*...*/}

and here
Quote:
for(j=0;j<=2;j++){/*..*/}

You are assuming the array has three items, namely e[0], e[1] and the not existing e[2].
I see a problem...

[update]
Quote:
scanf("%s",&e[i].ename);

This is another mistake: you never allocated memory for the ename string.
You either define it as an array of characters, e.g
C
struct emp
    {
    int eid;
    int sid;
    char ename[64];
    };

(assuming 64 is a resonable maximum size for your strings)
or use the malloc function to allocate dynamic memory (that you have to release at the end), e.g.
C#
printf("enter name of employee %d\t",i+1);
        e[i].ename = (char *) malloc(64 * sizoef(char)); // assuming again 64 is a 'good enough' max length
        if (! e[i].ename)
        {
          // to do: handle malloc failure
        }
        scanf("%s",&e[i].ename);

[/update]
 
Share this answer
 
v2
Comments
jithu ajith 10-Sep-14 8:39am    
i changed the 2 for loop to for(i=0;i<2;i++) and for(j=0;j<2;j++)

enter name of employee 1 jithu
enter supervisor id of employee 1
2
enter name of employee 2 jeev
enter supervisor id of employee 2
22
Segmentation fault (core dumped)



the error yet not corrected..
sry that was a mistake during debugging;
i rectified it now core dump occured

new
output


enter name of employee 1 jith
enter supervisor id of employee 1
8
enter name of employee 2 jeev
enter supervisor id of employee 2
9
enter name of employee 3 jay
enter supervisor id of employee 3
11
Segmentation fault (core dumped)
[bats@localhost c]$
 
Share this answer
 

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