Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in struct pointer

Status
Not open for further replies.

sujin

Programmer
Jul 20, 2002
5
0
0
IN
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(&quot;Enter ur name\n&quot;);
scanf(&quot;%s&quot;,&e->name);
int len = strlen((char*)&e->name);
printf(&quot;%d\n&quot;,len);
printf(&quot;Enter ur id\n&quot;);
scanf(&quot;%d&quot;,&e->id);
printf(&quot;Your name is <%s> and id is <%d>\n&quot;,&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.
 
you have not allocate any memory for &quot;name&quot; and also it's a good idea to free the memory that you have allocate dynamically.In the present case you should free the memory allocated to &quot;e&quot; in the end of the program.

here is your program:


#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));
e->name = (char*)malloc( sizeof(char)*25 );
printf(&quot;Enter ur name\n&quot;);
scanf(&quot;%s&quot;,e->name);
int len = strlen(e->name);
printf(&quot;%d\n&quot;,len);
printf(&quot;Enter ur id\n&quot;);
scanf(&quot;%d&quot;,&e->id);
printf(&quot;Your name is <%s> and id is <%d>\n&quot;,e->name,e->id);
free(e);
}

now it should work !
 
thanks a lot Leibnitz. It works fine.

SJ.
 
Hi,
you could always change your structure to be....

typedef struct Employee
{
char name[25];
int id;
}Emp;


which is esentially the same thing, but it saved the additional malloc for the name space. in reality the point of making it it a pointer means you can allocate only what is needed to store the users input. therefore the second malloc should be down after you ask the user for input.


char temp[25];

printf(&quot;Enter ur name\n&quot;);
scanf(&quot;%s&quot;,&temp);
int len = strlen(temp);
e->name = (char*)malloc( len+1 ); /* don't forget the \0 */
strcpy(e->name,temp);


----

 
Don't we need to free the memory allocated for name?

Does
free(e)

free the memory allocated to name too?
 
Don't we need to free the memory allocated for name?

Does
free(e)

free the memory allocated to name too?
 
Don't we need to free the memory allocated for name?

Does
free(e)

free the memory allocated to name too?
 
Don't we need to free the memory allocated for name?

Does
free(e)

free the memory allocated to name too?
 
We have to free the memory allocated to name seprately with a
free(e->e_name)

But we have to ensure we call this free before we call the free(e)
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top