[plug] [OT] Arg!! C Pointers on Char Arrays

John Usher john.usher at swiftdsl.com.au
Sat Feb 19 11:02:54 WST 2005


...

> The problem is putting the data into the array. I understand that I have
> an array of pointers and word is also a pointer. SO by doing it like I
> have I am copying the pointer so I end up with all the muppets pointing
> to the same bit of memory which is the # as that is the last character.
> This I obviously don't want. I want to copy the data not the pointer.
> Every combination of * & and -> that I have tried fails. I also tried
> strcpy but got nowhere: (All below failed)
>         strcpy(muppets[count].name,word);
>         &muppets[count].name = word;
>         &muppets[count].species = word;

What you really need to do is to get 'name' to point to a copy of 'word'

muppets[count].name = strdup(word) is probably what you want.

Note this will malloc space for name, so when destroying your muppets array,
you need to free(muppets[count].name)

...John...




More information about the plug mailing list