If i print (t->name) the result is correct but if i want the strlenCode:typedef struct robot { char *name; struct robot *next; } robot; robot *t;
(t->name) i have a memory fault
Why?!
If i print (t->name) the result is correct but if i want the strlenCode:typedef struct robot { char *name; struct robot *next; } robot; robot *t;
(t->name) i have a memory fault
Why?!
You need to give name some room to point to. Like this
t-> name = malloc(512 * sizeof(char));
/* then */
strcpy(t->name, "robot name");
then strlen() should work and printing it will too. Be sure to check return values also.
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
i doubt that it will give you the correct result with printing t->name.. the reason is that
1) you are not initializing the pointer name with any string.
2) if you are initializing it, then i guess that strlen should not give you any problems with it, it should work.
3) you also need to point 't' to a struct variable. otherwise, if it is not pointing to something, the program will crash, otherwise, if you dont want it to point to something, then you can go in for dynamic allocation of memory as chrismiceli said. but make sure that there is memory allocated for name, else the program will crash. with all this done, strlen should work.
Last edited by PING; 02-17-2005 at 09:15 AM.
Code:>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.