I have a nested array of structures in the following form:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char* e_name;
char* e_lastname;
}emp_name;
typedef struct
{
emp_name name;
int id;
}emp;
int main(int argc, char *argv[])
{
int i;
int cod=100;
emp job[3];
for (i=0;i<3;i++)
{
scanf("%s",&job[i].emp.e_lastname);
job[i].id=cod;
cod++;
}
for (i=0;i<3;i++)
{
printf("%s",job[i].emp.e_lastname);
printf("%d\n",job[i].id);
}
system("PAUSE");
return 0;
}
but the program hangs in the printing part, why is that?
Thanks
You have three problem:
First you want to access:
not
Second You should have:
instead of
You do not pass
&since it is an array you are passing to the scanf function.Third problem you should allocate memory to your
char *e_lastnameandchar *e_namecamps of thestruct emp_name.Note that:
scanf
int scanf ( const char * format, … );
So you want this:
Consider the fact that using scanf is unsafe, because:
Nevertheless, their are some workaround that you can do to use scanf (check them here)
Instead of scanf you can use fgets. fgets allows you to limit the data that will be placed in your buffer.