I'm not able to get the correct output, but the program seems to be fine. When I enter "sujin" as name and "100" as id, it outputs me only the first four characters of the name(5th char is some junk) and id. What would be the problem?.
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct Employee
{
char *name;
int id;
}Emp;
void main()
{
Emp *e;
e = (Emp*)malloc(sizeof(Emp));
printf("Enter ur name\n"
scanf("%s",&e->name);
int len = strlen((char*)&e->name);
printf("%d\n",len);
printf("Enter ur id\n"
scanf("%d",&e->id);
printf("Your name is <%s> and id is <%d>\n",&e->name,e->id);
}
output:
Enter ur name
sujin
String length is 5
Enter ur id
100
Your name is <suji(some junk character)> and id is <100>
Character 'n' is not being displayed properly.
SJ.
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct Employee
{
char *name;
int id;
}Emp;
void main()
{
Emp *e;
e = (Emp*)malloc(sizeof(Emp));
printf("Enter ur name\n"
scanf("%s",&e->name);
int len = strlen((char*)&e->name);
printf("%d\n",len);
printf("Enter ur id\n"
scanf("%d",&e->id);
printf("Your name is <%s> and id is <%d>\n",&e->name,e->id);
}
output:
Enter ur name
sujin
String length is 5
Enter ur id
100
Your name is <suji(some junk character)> and id is <100>
Character 'n' is not being displayed properly.
SJ.