newbie question about strings

This is a discussion on newbie question about strings within the C Programming forums, part of the General Programming Boards category; Hey, I have a quick question. Looking at this code: Code: char *string; char line[80]; strcpy(line, string); this gives me ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    newbie question about strings

    Hey, I have a quick question.

    Looking at this code:

    Code:
    char *string;
    char line[80];
    
    strcpy(line, string);
    this gives me a segmentation fault. How can I copy a type char * into a type char?

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Because you haven't allocated any storage for string; as of now it's a pointer to a char not an array of char.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Okay, so if I have something like:

    Code:
    string = strtok(line," \n\t");
    how could I copy the value pointed to by "string" into a variable of type char?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    I was just using example names. You can replace the line with whatever.

    Code:
    char *string;
    char line[80];
    char whatever[80];
    
    while(fgets(line,80,fin) != NULL) {
          string = strtok(line, " \n\t");
          strcpy(whatever, string);
    }
    segmentation fault.
    How do I get the stuff that is pointed to by "string", into "whatever"?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Since source and destination arrays are not the same strcpy() works; its behaviour is undefined if they overlap. Is whatever[] as big as line[]??

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by matsp View Post
    How about checking that the strtok() returned value (string) is not NULL?

    --
    Mats
    Is that why it was bombing? I was trying to input NULL in the char variable?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by itCbitC View Post
    Since source and destination arrays are not the same strcpy() works; its behaviour is undefined if they overlap. Is whatever[] as big as line[]??
    Yeah, whatever[80], line[80];

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by gp364481 View Post
    Is that why it was bombing? I was trying to input NULL in the char variable?
    Yep, if the return value of strtok() is NULL meaning string is a NULL pointer.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Okay, that was subtle. Thanks guys.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. Newbie question about string processing
    By prihod in forum C Programming
    Replies: 6
    Last Post: 04-15-2008, 10:14 PM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. Newbie strings question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2002, 03:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21