mercredi 10 novembre 2021

I am getting the warning for a piece of code, but the c++ book by bjarne stroustrup is saying it should be an error. what is right here?

As per book The C++ Programming Language, 4th Edition -

In C and in older C++ code, you could assign a string literal to a non-const char∗:

void f()
{
    char∗ p = "Plato"; // error, but accepted in pre-C++11-standard code
    p[4] = 'e'; // error : assignment to const
}

It would obviously be unsafe to accept that assignment. It was (and is) a source of subtle errors, so please don’t grumble too much if some old code fails to compile for this reason.

It suggest that, above code should give error, but I am getting a warning instead.

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

Aucun commentaire:

Enregistrer un commentaire