Results 1 to 3 of 3

Thread: illegal operation

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    I can't figure out if it is legal to allocate space as to say:

    Code:
    void combinestr(char *str1[80], char *str2[80]);

    I get a illegal operation from this code but I can't find a way to allocate space in a buffer for the characters.

    Code:
    class stringop {
    public:
    void combinestr(char *str1, char *str2);
    };
    void stringop::combinestr(char *str1, char *str2)
    {
    strcat(str1,str2);
    cout << str1;
    }
    Matt

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Pass a pointer to the first pointer, and then reallocate it using new:
    Code:
    void combinestr(char **str1, char *str2) {
        char *pcTemp = new char[strlen(*str1)+strlen(str2)+1];
    
        strcpy(pcTemp, *str1);
        strcpy(pcTemp+strlen(*str1), str2);
        pcTemp[strlen(*str1)+strlen(str2)] = 0;
    
        delete[] (*str1);
        *str1 = pcTemp;
    }
    However, this MUST be used with a pointer, not an array, and it must have been allocated with new. String manipulations in C are notoriously vicious, but here's an example:
    Code:
    char *pcStr = "Hello";
    char *pcOther = new char[strlen(pcStr)+1];
    strcpy(pcOther, pcStr);
    // Now prepared for use
    combinestr(&pcOther, pcStr);
    cout << pcOther << endl;
    Easiest way to use strings is the string class from the STL -- it takes virtually no more CPU time than doing it yourself.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Thanks alot parksie!
    Matt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width