Page 1 of 1

Interesting char *

PostPosted: Jan 24, 2003 @ 2:36am
by brendan
I've recently been having issues with chars...
I have the following struct

struct player_attribs {
char *name;
};

there is other stuff in it as well, anyhow....
I then:

player_attribs player[20];

do I still have to

player[5].name = new char[10] = "1234567890";

or not?

it "seems" I can just:

player[5].name = "fred";

your comments?

PostPosted: Jan 24, 2003 @ 3:45am
by MirekCz
you still have to do it.
Possibly your code is going very bad somewhere and the char *name structure is pointing to a part of memory you're using (ee... one of the player_attribs struct, maybe?:-)
So each time you save something there you're wildly writing all over this structure... or maybe somewhere else.. god knows... just don't do it;-)
you have to either have something like

char name[256];
char *namepointer;
namepointer=name;

or like you said, use new/malloc/whatever


btw think about it a little.. you create a pointer without giving size and your poor PPC should know how large your array is? :-)

Interesting char *

PostPosted: Jan 24, 2003 @ 5:52am
by Dan East

Interesting char *

PostPosted: Jan 24, 2003 @ 12:20pm
by andys

PostPosted: Jan 24, 2003 @ 2:19pm
by brendan
hmmm, yes Dan, you could be right... I didn't test the new char[5] = "12345"; idea etc....

I was having issues when pre defining them to a size...

i.e the command

player[5].name = "12345"; failed, because cant assign char[5] to *char[5] which now makes perfect sence.... lol

but the code was "working" by just leaving it as char *name

then just saying

player[5].name = "fred";

but I think I'll just "fix it" to the prefered method... and yes the null (\0) is a valid point....

it could be the the point was just renamed to the "name"?

PostPosted: Jan 25, 2003 @ 1:59am
by Sm!rk

PostPosted: Jan 25, 2003 @ 12:42pm
by gamefreaks

PostPosted: Jan 25, 2003 @ 2:50pm
by Hosed

PostPosted: Jan 25, 2003 @ 5:25pm
by Orpe