1

I want to pass a char array to my constructor and initialize the member variable field, but I get the following error:

error: incompatible types in assignment of 'char*' to 'char [255]'

class C
{
    char field[255];

public:
    C(char field[255])
    {
        this->field=field;
    }
};
2
  • I recommend using different names between your constructor and function arguments and your data members. Makes life easier. Commented Jun 12, 2020 at 20:17
  • Also, prefer using std::string instead of char arrays. Arrays can overflow. The std::string can be passed by reference. Commented Jun 12, 2020 at 20:19

4 Answers 4

2

You can't assign/copy an array like that in c++. Instead, you could use std::copy to do a copy of the array, in the constructor:

C(char f[255])
{
  std::copy(f, f + 255, field);
}

I suggest using std::string instead of char[255]. Then your class simply becomes:

class C
{
  std::string field;
 public:
  C(std::string field) : field(std::move(field)) {}
};

This way you don't have to worry about the array being large enough.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't copy an C array with the assignment operator but C++ provides std::array:

class C
{
    std::array<char, 255> field;

public:
    C(const std::array<char, 255> &field) : field(field) {}
};

If you can use dynamic memory allocation and the size of the field can also be dynamic you should prefer std::string:

class C
{
    std::string field;

public:
    C(const std::string &field) : field(field) {}
};

2 Comments

The C++ language provides other opportunities, such as std::string and std::vector<char>. I recommend std::string.
@ThomasMatthews I'm trying to make my answer as similar as possible. There is no dynamic memory allocation in the question so I avoid it if possible. This could be code for an embedded system like an Arduino. Sometimes it makes sense to avoid std::string.
2

In a function parameter, a char[] (with or without a number specified) is just syntax sugar for a char* pointer. And you can't initialize a char[] array with a char* pointer, hence the compiler error.

For what you are attempting, you must copy the memory that the char* is pointing at, eg:

class C
{
    char field[255];

public:
    C(char field[255]) // aka C(char *field)
    {
        memcpy(this->field, field, 255);
        // or: std::copy(field, field+255, this->field);
        // or: std::copy_n(field, 255, this->field);
    }
};

Alternatively, if you want to ensure the caller can only pass in a char[255], pass it by reference instead of by pointer:

class C
{
    char field[255];

public:
    C(const char (&field)[255])
    {
        memcpy(this->field, field, 255);
        // or: std::copy(field, field+255, this->field);
        // or: std::copy_n(field, 255, this->field);
    }
};

That being said, you should consider using std::array instead, then you can use operator= assignment like you want:

#include <array>

using Char255Array = std::array<char, 255>;

class C
{
    Char255Array field;

public:
    C(const Char255Array &field)
    {
        this->field = field;
    }
};

Or better, use the constructor's member initialization list:

#include <array>

using Char255Array = std::array<char, 255>;

class C
{
    Char255Array field;

public:
    C(const Char255Array &field)
        : field(field)
    {
    }
};

Comments

-2

Raw arrays are not recommended to use in c++, you can read about vectors and arrays classes they are more usefull. Although the solution for your problem is you just need to change the type of the attribute field to char*, and it works perfectly. for more explanation: in c++, you can assign to a char* type a char array but the reverse is impossible because the pointer is only pointing on the first character.

class C
{
    char* field;

public:
    C(char field[255])
    {
        this->field=field;
    }
};

6 Comments

He will point at the first character of the char array given in the parameters of the constructor in this case he will be pointing at field[0] memory adress
But where did that memory come from? Did someone do new? Who's supposed to do delete?
In the main method when the instance is created, then we can call the constructor with new
And who calls delete? What happens when a C object is copied? There are many potential issues here, and is not good advice to solve the OP's problem.
I think it's an easy solution that can help him to keep working with the char type instead of string, because having a pointer that point on the same is string is much more optimized than having two char arrays with the same information in the memory, after he can add a destructor to call delete method. This is my point of view. He can choose whatever solution he want after all
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.