Compiler and linker errors

C++ "invalid conversion from 'const char*' to 'char'"

This conversion error means code supplies a string pointer where one character is required. Double quotes create a string literal; single quotes create a char literal. MSVC reports the initialization case as C2440. It reports an incompatible function argument as C2664.

Distinguish a character from text

A char stores one character. A string literal such as "A" has an array type that converts to a pointer to constant characters in many expressions. It is not one char. The diagnostic names the source and destination types, which tells you to inspect the quotes or function parameter. GCC names const char* as the source type and char as the destination type. MSVC reports the initialization case as C2440 and an incompatible function argument as C2664.

Failing C++17 example

#include <iostream>

int main() {
    char grade = "A";
    std::cout << grade << '\n';
    return 0;
}

GCC diagnostic core:

error: invalid conversion from 'const char*' to 'char'

MSVC reports this initializer mismatch as C2440.

Fixed example

#include <iostream>

int main() {
    char grade = 'A';
    std::cout << grade << '\n';
    return 0;
}

Expected output:

A

Use single quotes for one character

Write char grade = 'A'; when the value is one character. Write std::string grade = "A"; when the value is text. A character literal can represent an escape such as '\n'. Do not use a multi-character string literal to initialize char, and do not assume that taking its first character is the intended behavior. Choose the type based on what later code needs to do.

Match the function parameter

A function taking char accepts one character, not a whole word. If the function should process a word, change its parameter to const std::string& or const char*. If it truly needs one character, pass a char such as text[0] only after checking that the text is nonempty. Changing a parameter type may affect every call site, so make the function’s contract explicit.

Failing C++17 example: pass a string literal to a char parameter

#include <iostream>

void printGrade(char grade) { std::cout << grade << '\n'; }

int main() {
    printGrade("A");
    return 0;
}

GCC diagnostic core:

error: invalid conversion from 'const char*' to 'char'

MSVC reports this argument mismatch as C2664. Change the parameter to accept text, or pass one character when that is the intended input.

Fixed example

#include <iostream>

void printGrade(const char* grade) { std::cout << grade << '\n'; }

int main() {
    printGrade("A");
    return 0;
}

Expected output:

Ada

Pass a C string when an API needs one

A std::string owns text and is not implicitly a const char*. When a C API requires a null-terminated character pointer, use text.c_str(). The pointer remains valid only while the string stays alive and is not modified in a way that invalidates its storage. Prefer std::string in C++ interfaces and convert at the boundary where a C API requires it.

Do not store a string literal in char*

In C++11 and later, a string literal cannot initialize a writable char*. GCC reports ISO C++ forbids converting a string constant to 'char*' with -Wwrite-strings; -pedantic-errors makes it an error. MSVC rejects it with C2440 when you use /permissive-. This example follows GCC's warning model, so it can run safely because it only prints the text.

Warning example

#include <iostream>

int main() {
    char* name = "Ada";
    std::cout << name << '\n';
    return 0;
}

GCC diagnostic core:

warning: ISO C++ forbids converting a string constant to 'char*'

Use const char* when you need a read-only C string, or std::string when you need an owning string value.

Fixed example

#include <iostream>

int main() {
    const char* name = "Ada";
    std::cout << name << '\n';
    return 0;
}

Expected output:

Ada

Do not silence the diagnostic with a cast

A reinterpretation or cast does not turn a sequence of characters into one character. It can discard information or create a pointer with an invalid lifetime. Read the destination type in the function declaration and decide whether the program needs one character, a C string, or a C++ string. Then change the value or the parameter to match.

Exercise

Make the program pass a C string to printName, which expects const char*. Use the string’s null-terminated character data.

Broken program

#include <iostream>
#include <string>

void printName(const char* name) { std::cout << name << '\n'; }

int main() {
    std::string name = "Ada";
    printName(name);
    return 0;
}

GCC diagnostic core for this related case:

cannot convert 'std::string'

This is a different message from the const char*-to-char error. MSVC reports this argument mismatch as C2664.

Sample input: none.

Expected output after the fix:

Ada

Hint 1: Compare the argument type with the parameter type. Hint 2: Convert std::string at the C API boundary with c_str().

Show one possible solution
#include <iostream>
#include <string>

void printName(const char* name) { std::cout << name << '\n'; }

int main() {
    std::string name = "Ada";
    printName(name.c_str());
    return 0;
}

Expected output:

Ada

Keep learning with CppPath

Continue with the Variables & Types lesson. The full lesson includes a quiz and a checked coding challenge with a free account.