Simple C++ Question [Dynamic char allocation]

Discussion in 'Programming' started by Barti1987, Jan 15, 2009.

  1. #1
    I am trying to allocate new character string:

    
    char *mystring;
    mystring = new char[maxInput];
    
    Code (markup):
    When I try to send it to a function by value, it won't work:

    
    
    foo(&mystring);
    
    
    void foo(char *string)
    {
       *string = "test";
    }
    
    Code (markup):
    Error that I get is:

    cannot convert `char**' to `char*' for argument `1' to `void foo(char*)

    Thanks for any hints.

    Peace,
     
    Barti1987, Jan 15, 2009 IP
  2. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    since you have declared mystring to by a char * you do not need to use the address symbol when passing it to the foo function ... it is already a pointer type..

    so you should call it like

    foo(mystring);
    Code (markup):
    no & needed..
     
    gnp, Jan 15, 2009 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    I tried the following:

    
    foo(mystring);
    void foo(char *string)
    {
       *string = "test";
    }
    
    Code (markup):
    Then I got an error : invalid convert from const char* to char.

    So its trying to convert from a pointer to a char, which is invalid. To fix it, I did this:

    
    foo(mystring);
    void foo(char *string)
    {
       string = "test";
    }
    
    Code (markup):
    Which loads OK but the value of mystring isn't actually changing.

    Any ideas?

    Thanks again,
     
    Barti1987, Jan 16, 2009 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Let me ask a simpler question, it should clear things better for me:

    
    char *myString;
    myString = new char[maxSum];
    
    Code (markup):
    The first line, we create a pointer to char?
    The second line is creating a dynamic char in the pointer or address?

    So basically:

    mystring = ? (I think the value?)
    &mystring = ? (I think the address location)
    *mystring = ?

    Thanks,
     
    Barti1987, Jan 16, 2009 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Finally got it to work.. Just changed the declaration to:

    void foo(char **string);

    Peace,
     
    Barti1987, Jan 16, 2009 IP
  6. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #6
    myString is the address.
     
    it career, Jan 16, 2009 IP
  7. msrmani

    msrmani Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,
    you want to work with strings in c++ so you can define some useful functions like this.

    char* CreateString(char* source)
    {
    int size = strlen(source); // get the length of source string
    char* prov = new char[size + 1]; //allocate a buffer with exact required size
    strcpy(prov, source);
    prov[size] = 0; // make the string NULL terminate
    return prov;
    }

    to call the function
    char* mystring = CreateString("New string");
    the variable mystring now conaines the string "New string".
    hope that gonna be helpful
     
    msrmani, Jan 23, 2009 IP