Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I properly assign a char* in c++?

My c++ code looks like this:

char* x;
    switch(i){ 
        case 0:
            x = '0';
        case 1:
            x = "1";
        ...}

I can't figure out how to make this work because for the first x = '0'; the compiler complains with:

error: invalid conversion from ‘char’ to ‘char*’

and for the second x = "1"; the compiler complains with:

warning: deprecated conversion from string constant to ‘char*’

What should I do here? Am I going about this completely wrong?

like image 425
wfbarksdale Avatar asked Nov 05 '25 17:11

wfbarksdale


2 Answers

In case 0 you're trying to set x to a character (of type char), but in case 1 you're trying to set x to a C string (of type char const[2]). It's the type of quotes that make a difference; single-quotes are for characters, and double quotes are for C-style strings.

If you're meaning to set it to a string both times, put double quotes around the 0 in x = '0'.

If you're meaning to set x to a character, use single quotes both times and dereference the pointer, like *x, so that it becomes *x = '0', or *x = '1', or change the type of x from char* (pointer to character) to char (character). Then you don't have to dereference it.1

Then again, if you are trying to set x to a string, it would be better to use a C++ string instead of a C string, with std::string. Then you'd do it with double quotes like a C string, but you'd get a bevvy of extra features like automatic memory management, bounds checking, and all the member functions that it has.

1 As Nicolas Grebille pointed out: before doing that, make sure it points to a valid char, either using new:

char* x = new char;

or by creating a char on the stack:

char c;
char* x = &c;

Important:

If you're going to use a char* with strcat later (or with any function expecting a C-string), you must properly NULL terminate your buffer. So you need to do it either this way:

char x[2] = {}; // on the stack, filled with NULLs
                // use a bigger number if you need more space

or

char* x = new char[2]; // on the heap, use more than 2 if you're
                       // storing more than 1 character
x[1] = NULL; // set the last char to NULL

If you don't do that, you'll get garbage if you're unlucky or a segfault if you are lucky.

Then after you declare x as the above, you can do *x = '0' or whatever.

If you decide to use new[], make sure you deallocate the memory with a corresponding delete[].

like image 50
Seth Carnegie Avatar answered Nov 07 '25 10:11

Seth Carnegie


Contrary to popular belief, char* is not a string. Use std::string instead.

#include <string>

std::string x;
switch(i){ 
    case 0:
        x = "0";
    case 1:
        x = "1";
    ...}
like image 25
André Caron Avatar answered Nov 07 '25 09:11

André Caron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!