Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert char* to const char* in C++

How to convert char* to const char* in C++? Why program 1 is working but program 2 can't?

Prog 1 (working):

char *s = "test string";
const char *tmp = s;
printMe(tmp);

void printMe(const char *&buf) {
    printf("Given Str = %s", buf);
}

Prog 2 (not working)

char *s = "test string";
printMe((const char *)s);     // typecasting not working

void printMe(const char *&buf) {
    printf("Given Str = %s", buf);
}

The error I receive:

x.cpp:10:15: warning: conversion from string literal to 'char *' is 
deprecated [-Wc++11-compat-deprecated-writable-strings]
char *s = "test string";
          ^
x.cpp:12:5: error: no matching function for call to 'printMe'
printMe(s);
^~~~~~~
x.cpp:6:6: note: candidate function not viable: no known conversion 
from 'char *' to 'const char *&' for 1st argument
void printMe(const char *&buf)
 ^
1 warning and 1 error generated.

Thanks.

like image 673
M. Rock Avatar asked Jun 19 '17 18:06

M. Rock


People also ask

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

How do I convert a char to const char?

You cannot explicitly convert constant char* into char * because it opens the possibility of altering the value of constants. To accomplish this, you will have to allocate some char memory and then copy the constant string into the memory. That is the only way you can pass a nonconstant copy to your program.

What does char * const mean in C?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.

What is the difference between const char * and char * const?

The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).


1 Answers

printMe takes an lvalue reference to a mutable pointer to const char.

In your first example, tmp is an lvalue of type mutable pointer to const char, so a reference can be bound to it without issue.
In your second example, (const char*)s creates a temporary const char* object. Lvalue references to mutable objects can't bind to temporaries, so you get an error. If you change printMe to take a const char* const& then the call will succeed with or without the explicit cast.

void printMe(const char * const& buf) {
    printf("Given Str = %s", buf);
}

int main() {
    char s[] = "test string";
    printMe(s);
}

Live on Coliru

Of course, if you don't want to alter the object (the pointer) passed into printMe, then there's no reason to use a reference at all. Just make it take a const char*:

void printMe(const char * buf) {
    printf("Given Str = %s", buf);
}

int main() {
    char s[] = "test string";
    printMe(s);
}

Live on Coliru

In the end, this is the same reason something like this:

void doSomething(const std::string& s) {}
int main() {
    doSomething("asdf");
}

works while this:

void doSomething(std::string& s) {}
int main() {
    doSomething("asdf");
}

does not. A temporary object is created, and the reference to non-const object can't bind to the temporary.

like image 80
Miles Budnek Avatar answered Sep 16 '22 18:09

Miles Budnek