|
Passing an Array as an Argument |
Ron Rider
Member #8,208
January 2007
|
I'm having trouble getting a class method from assigning an array parameter to a private array variable. I have the following array of 24 char elements declared: char pl_Name[24] = ""; And I am trying to pass that same array to a function in my 'Character' class. However, I receive the following error from Dev-C++: Quote: incompatible types in assignment of `char*' to `char[24]' I get the error when I try to assign an array to another array. Here is the line of code with my error; it is the constructor for my class: Character(char name[]) { m_name = name; } The method receives the array just fine, but when I try to assign the name array to the private class variable m_name I receive the incompatible type error. I understand that arrays are always passed by reference, but I am unclear about how that should change my code. |
BAF
Member #2,981
December 2002
|
Try char *name. |
Goalie Ca
Member #2,579
July 2002
|
Tip: Use c++ strings if you can. ------------- |
James Stanley
Member #7,275
May 2006
|
Don't use C++ Strings. |
Joel Pettersson
Member #4,187
January 2004
|
I am guessing you have a char m_name[24]; in the class. Correct? (if not, note so, as this assumes it to be the case. not enough info in your post to be wholly sure) You do not copy C-style arrays by assignment. If that's what you're trying to do (copy), use one of the string copying functions (str*cpy). (and for other kinds of arrays, there is the memcpy function. a loop can also be used) If you know the length of the arrays match or the destination is always atleast as large as that copied from, you can use strcpy(m_name, name); otherwise (or bad things might happen), use strncpy(m_name, name, whatever_the_length_of_m_name_is); m_name[whatever_the_length_of_m_name_is - 1] = '\0'; where the latter of the two lines ensure the string is null-terminated if the whole string is overwritten. I'd recommend doing thus in this case as this character array has not previously been initialized. In cases where the elements have been initialized to all 0s (as in the number, not the character '0') or '\0' (amounts to the same value - 0) - or atleast the very last character remains so - you can copy a maximum length one less of the size, knowing that a proper 0 will remain, and ommit the latter line. Using these C-string operation functions will (unless you include something else that includes the file or declares them) require a #include <cstring> somewhere before the code in question. And, this being C++, you'll need to either put using namespace std; afterwards or prefix the those function names with std::
|
BAF
Member #2,981
December 2002
|
Oh yeah, I forgot. C++ strings would GREATLY simplify and make this much safer. |
Goalie Ca
Member #2,579
July 2002
|
Quote: Don't use C++ Strings. Care to explain why? ------------- |
GullRaDriel
Member #3,861
September 2003
|
It loads a ton of shit. "Code is like shit - it only smells if it is not yours" |
Ron Rider
Member #8,208
January 2007
|
Joel Pettersson: Thank you for your reply, using the strcpy() function compiled without error. About C++ strings, I have been trying to get strings working with DIALOG arrays, but I cannot seem do so. I can't get anything other than an array of chars to be read from my d_edit_proc and actually change without causing my program to crash. Can I use other data types with my DIALOGS? Also, I want my strings to have a set size on them; I'm not sure if I can do that with strings or not. |
Goalie Ca
Member #2,579
July 2002
|
------------- |
Ron Rider
Member #8,208
January 2007
|
Ah, thank you for that. I can't wait to implement c_str() I have another question, I'm hoping someone might be able to help me with. I have a d_edit_proc that could read my array of chars; the code below: { d_edit_proc, 30, 50, 270, 16, 0, 0, 0, 0, 24, 0, pl_Name, NULL, NULL } I have 3 more d_edit_proc's declared, each using a different variable of the same data type to edit. pl_Name used to be declared like this: char pl_Name[24] But I have changed pl_Name to be of the string data type. I can use c_str() and get my d_edit_proc to read the string, but I encounter some issues. The editable text-box I am now using looks like this: { d_edit_proc, 30, 50, 270, 16, 0, 0, 0, 0, 24, 0, (void*)pl_Name.c_str(), NULL, NULL },
My Problems |
Fladimir da Gorf
Member #1,565
October 2001
|
Are you sure that you're keeping the name stored somewhere (that it's not just a local variable in a small method)? OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
ImLeftFooted
Member #3,935
October 2003
|
Quote: Ah, thank you for that. I can't wait to implement c_str() But... huh? ... c_str() is already implemented as std::string::c_str() |
Kibiz0r
Member #6,203
September 2005
|
I think he just meant "start using it". --- |
BAF
Member #2,981
December 2002
|
c_str() is a const char*, meaning you can't modify it. You will have to use a char array with d_edit_proc, or write your own version that works with std::String. |
|