Hey, I have a quick question.
Looking at this code:
this gives me a segmentation fault. How can I copy a type char * into a type char?Code:char *string; char line[80]; strcpy(line, string);
Thanks
Hey, I have a quick question.
Looking at this code:
this gives me a segmentation fault. How can I copy a type char * into a type char?Code:char *string; char line[80]; strcpy(line, string);
Thanks
Because you haven't allocated any storage for string; as of now it's a pointer to a char not an array of char.
Okay, so if I have something like:
how could I copy the value pointed to by "string" into a variable of type char?Code:string = strtok(line," \n\t");
Your code is breaking up the array line[] into tokens separated by <spaces> or <newlines> or <tabs>, returning a pointer to it and then inserting it back into the same array line[]. So in a nutshell source and destination is the same implying that it's copying it onto itself.
I was just using example names. You can replace the line with whatever.
segmentation fault.Code:char *string; char line[80]; char whatever[80]; while(fgets(line,80,fin) != NULL) { string = strtok(line, " \n\t"); strcpy(whatever, string); }
How do I get the stuff that is pointed to by "string", into "whatever"?
How about checking that the strtok() returned value (string) is not NULL?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Since source and destination arrays are not the same strcpy() works; its behaviour is undefined if they overlap. Is whatever[] as big as line[]??
Okay, that was subtle. Thanks guys.
I have to wonder, since you did not initialize "string," what exactly you thought this would copy. The meaning of life, perhaps?
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}