Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

argument of type "const char *" is incompatible with parameter of type "char *" & expression must be a modifiable lvalue

#include <iostream>
class House {
private:
    char name[20];
    char date[10];
    float width;
    float height;
    float age;
public:
    void set(char n[20], char d[10], float w, float h, float a) {
        name = n;
        date = d;
        width = w;
        height = h;
        age = a;
    }

    void get() {
        std::cout << name << ", " << age << " (" << width << "x" << height <<
            ")" << ", age = " << age << std::endl;
    }
};

int main() {
    House shop;
    shop.set("Shop", "2000.10.10", 200.5, 100.4, 10.0);
    return 0;
}

The picture shows my errors in debugging:
enter image description here

I know that I can use std::string, but in this program I want to use char string[n]
How to fix my mistakes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The short answer is to use std::string instead. It’s easier and safer.

But if you must use arrays, keep in mind that you cannot assign an array to another like that. What you must instead do is copy elements from one array to another. Since you’re dealing with char arrays, the obvious solution is to use strcpy:

#include <cstring>

void set(const char n[20], const char d[10], float w, float h, float a) {
    strcpy(name, n);
    strcpy(date, d);
    width = w;
    height = h;
    age = a;
}

Notice how I made the char array arguments const: that’s because array arguments to functions are really actually pointers in disguise, and you can’t legally assign a literal to a non const pointer (at least not without enabling some kind of compiler specific option to allow dangerous deprecated conversions).

This, however, is potentially dangerous: it assumes that the input array is null terminated, and also that there are not more characters in the source that can fit in the destination. That includes the null terminator, which C++ will helpfully put at the end of any char array literal: so, a string like "2000.10.10" actually needs 11 characters, because of the hidden \0 at the end, and missing that \0 will cause a lot of trouble down the line because every C string function assumes it is there (and if it isn’t, you’re in undefined behaviour territory).

For all those reasons, using char arrays is a minefield, so please use std::string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading