Passing an Array as an Argument
Ron Rider

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

Try char *name.

Goalie Ca

Tip: Use c++ strings if you can.

James Stanley

Don't use C++ Strings.

Joel Pettersson

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

Oh yeah, I forgot. C++ strings would GREATLY simplify and make this much safer.

Goalie Ca
Quote:

Don't use C++ Strings.

Care to explain why? ???

GullRaDriel

It loads a ton of shit.

Ron Rider

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

string.c_str() returns 'const char*'.
ie:

string c("World!");
printf("Hello %s", c.str());

Ron Rider

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
1. When I edit one text-box and then mouse over another, it copies the information from the first one, and puts that into the second one.
2. After I am finished with the dialog and I have entered all the data, all 4 variables hold the same data as my last variable.

Fladimir da Gorf

Are you sure that you're keeping the name stored somewhere (that it's not just a local variable in a small method)?

ImLeftFooted
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

I think he just meant "start using it".

BAF

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.

Thread #591424. Printed from Allegro.cc