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,
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..
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,
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,
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