Thread: strcpy(pstring, function())

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    129

    strcpy(pstring, function())

    Now I have a function const char *get_string(size_t maxlen) where the significant part according to this problem is like

    Code:
    vector<char> str;
    ...
    char *s = new char[str.size()+1];
    
    /*	Copy the vector to passed string	*/
    for(size_t i = 0;  i < str.size();  i++)
    	s[i] = str[i];
    s[str.size()] = '\0';
    str.clear();
    return s;
    in main() I have

    Code:
    char *s;
    strcpy(s, get_string(4));
    printf("\n%s", s);
    There must be something wrong in the upper code 'cause if, in main(), I set s to NULL nothing seems to happen. What's going on?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    char *s;
    strcpy(s, get_string(4));
    printf("\n%s", s);
    This should give you a warning and a run-time error. You're trying to copy a string to unallocated memory. s is a pointer at this point, nothing more, so you need to point it to some memory before assigning to it.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char *s;
    >strcpy(s, get_string(4));
    >printf("\n%s", s);

    Should be:
    s = get_string(4);
    Last edited by swoopy; 03-01-2002 at 02:49 PM.

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > s = strcpy(get_string(4));

    No...

    char * strcpy(char *dest, char *source);

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    And after the edit, still no.

    You can't reassign strings like that.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Sorry about the editing. Since he didn't show the function heading, I didn't know the name of the function. And yes you can. Try it.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    And of course I have tried passing s from main o get_string:

    Code:
    void get_string(char *s, size_t maxlen)
    {
        ...
        if(s == NULL)
            s = new char[str.size()+1];
    
        /*	Copy the vector to passed string	*/
        for(size_t i = 0;  i < str.size();  i++)
            s[i] = str[i];
        s[str.size()] = '\0';
        str.clear();
    }
    But it remains the same. And now s points to a local variable...

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    void get_string(char **s, size_t maxlen)
    {
        ...
        if(*s == NULL)
            *s = new char[str.size()+1];
    
        /*	Copy the vector to passed string	*/
        for(size_t i = 0;  i < str.size();  i++)
            (*s)[i] = str[i];
        (*s)[str.size()] = '\0';
        str.clear();
    }
    
    //And to call function, use something like:
    char *s = NULL;
    get_string(&s,80);

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Ah! Could someone explain that code a bit? It works, alright but
    char s[n];
    getstr(s, m);
    doesn't - not that it's a problem that is easily fixed with some polymorphzation but why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM