Hi,
I have a function like this:
The char *word passed to the fn is a pointer to malloc spaced for the string.Code:void addidfword(char *word, char *dname, IDFWORDS *C) { IDFWORDS *new; IDFWORDS *ptr; new=(IDFWORDS *)malloc(sizeof(IDFWORDS)); new->word=word;
Each IDFWORDS struct has a char *word in it.
It works if instead of:
new->word=word;
I do:
new->word=(char *)malloc(sizeof(char)*strlen(word)+1);
strcpy(new->word,word);
But I am wondering why I have to malloc more space for something that is malloced already. Why can't I just set new->word equal to the pointer to the malloced space?
THanks.



LinkBack URL
About LinkBacks


