Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]

Tags:

c++

c

string

int ::  cadena calculatelenght(const cadena& a, const char* cad)
{
    cadena c;
    int lenght = 0;
    char* punt; punt = cad;
    while(*punt){
        lenght++; punt++;
    }
    return lenght;
}

I have this problem, I want to calculate the length of a C string without using functions like strlen, in other methods of my cadena class I can because is not const char*, but now I don't know what to do.

like image 463
freinn Avatar asked Feb 08 '12 19:02

freinn


People also ask

What does invalid conversion from const char* to char mean?

In C++, the error invalid conversion from 'const char*' to 'char*' happens when you try to pass a nonmodifiable constant char for char. C++ throws this error to let you know you are performing an operation that is not possible.

What is const char*?

const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated.


2 Answers

You can declare punt to be of the correct type:

const char * punt = cad;
like image 61
Kerrek SB Avatar answered Sep 29 '22 12:09

Kerrek SB


You need:

const char* punt; punt = cad;
like image 31
Joni Avatar answered Sep 29 '22 13:09

Joni