Hi rjhome,
It looks like the problem might be that you have a structure (tongxun) of uninitialized pointers to type char. When you allocate memory for a structure in your code
Code:
void info_input()
{
struct tongxun *p;
p = (struct tongxun*)malloc(sizeof(struct tongxun));
p->id = "97100456";
p->name = "rochelle hsu";
p->email = "[email protected]";
p->oicq = "17731056";
free(p);
return;
}
the pointer p ends up with only enough memory to hold the pointers which are up to this point uninitialized. I would suggest either one of two solutions, the first is defining your structure using character arrays ...
Code:
//define the struct tongxun
struct tongxun
{
char id[10];
char name[MAX_NAME]; /* you could even use constants */
char email[MAX_EMAIL];
char oicq[MAX_ICQ];
char home_phone[10 + 1]; /* standard number plus NULL */
char mobile_phone[10 + 1];
}; /* you forgot to close the structure with a semicolon */
or you can allocate the memory dynamically in info_input() ...
Code:
void info_input()
{
struct tongxun *p;
char *idtmp = "97100456";
if ((p->id = (char *)malloc(strlen(idtmp) + 1)) != NULL)
strcpy(p->id, idtemp);
... and so forth ...
This is not elegant but it does allocate sufficient memory for the stirng and not just the pointer.
I hope this helps.