Question

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?

Was it helpful?

Solution

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[].

OTHER TIPS

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";
    ...}

If you're going to use a char *, you need to allocate space for the string to be stored either by changing the declaration of x to something like:

char x[10]; // allocate a fixed 10-character buffer

or dynamically allocating the space on the heap:

x = new char[10]; // allocate a buffer that must be deleted later

then you can use:

strcpy(x, "1"); // Copy the character '1' followed by a null terminator
                // into the first two bytes of the buffer pointed to by x.

to copy the string "1" into the buffer pointed to by x. If you use the second example, you must later:

delete x;

Of course if you really want to deal with strings there are better ways, and there are better ways to deal with individual characters as well (see other answers).

It's more a remark that an answer, but you can get the chars 0, 1,... from the int with:

char x = '0' + i;

wich would avoid your switch statement (with 0 <= i <= 9 of course).

You're declaring a char pointer, not allocating any space, then trying to assign a character to it.

char x;

Would give you a variable of type char

char *x = new char[10]; 

would give you a pointer to 10 bytes of memory pointed at by a char pointer.

As noted by André Caron above, since you're using c++ you really should be using an actual string to make your life easier

First, please figure out a way that you can use std::string. If you continue on the path you have chosen, you will certainly create buggy programs.

Having said that, here is your answer: How you declare x, and how you assign to it, depends in large part upon how you later use it. Here is one example:

#include <stdio.h>
int main(int ac, char **av) {
  const char* x;      
  switch(ac) {
  case 0:
    x = "0";
    break;
  case 1:
    x = "1";
    break;
  default:
    x = "DANGER!";
    break;
  }
  printf("%s\n", x);
  return 0;
}

In this first example, we set the pointer x to point to one of three possible const char arrays. We can read from those arrays later (as in the printf call), but we can never write to those arrays.

Alternatively, if you wish to create an array you can later write to:

#include <stdio.h>
#include <string.h>
int main(int ac, char **av) {
  char x[32]; // make sure this array is big enough!     

  switch(ac) {
  case 0:
    strcpy(x, "0");
    break;
  case 1:
    strcpy(x, "1");
    break;
  default:
    strcpy(x, "NOT 0 nor 1:");
    break;
  }

  strcat(x, ", see?");
  printf("%s\n", x);
  return 0;
}

EDIT: Get rid of incorrect const from 2nd example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top