-5

I tried to set simple char with empty value without success. The main goal was to compare between std::string single char and pre-defined char

std::string str ="fcvfr";
char c = '' //trying to set empty char here ....  but it gives me error
if(c == str[0]) 
{
   //do something 
}

This leads me to the question when should I use each of the following types:

char * , char , char[] 
user63898
  • 29,839
  • 85
  • 272
  • 514
  • I may be stating the obvious, but shouldn't f be 'f' (with single quotes)? – Kakalokia Jul 23 '14 at 11:04
  • Also, you're missing a semicolon. And if _"it gives [you] error"_ it would be nice of you to specify exactly _what_ error you're getting. – Michael Jul 23 '14 at 11:05
  • 1
    What do you mean by "empty char"? A character always has a value. And what is `f`? Did you mean the character constant `'f'`? What is the error you get, and do you still get it if you add the missing `;`? – Mike Seymour Jul 23 '14 at 11:06
  • 1
    What do you want to know? I cannot see any connection between the last sentence and former sentents. – slyx Jul 23 '14 at 11:06
  • i fixed it it meant to be empty char , how can i set empty char into char variable , sorry – user63898 Jul 23 '14 at 11:09
  • @user63898 There's no such thing as an empty `char`. A `char` always contains exactly one character (or more precisely, exactly one small integral value usually interpreted to be a single byte character, or part of a multi-byte character). An empty string is either an `std::string` with a size of 0, or a `const char*` pointing to a `'\0'`. – James Kanze Jul 23 '14 at 11:29
  • @user63898 There's no such thing as an empty char. A char is just one byte in memory - if you want to initialize it to zero, do `char c = '\0'` or `char c = (char) 0`. – MGA Jul 23 '14 at 11:30
  • @user63898 And of course, you should never use `char*` or `char[]` except when interfacing legacy code (and even then, the `char*` will usually come from something like `&buffer[0]`, where `buffer` is a `std::vector`). – James Kanze Jul 23 '14 at 11:37
  • @JamesKanze Sorry for my earlier comment which was very similar to yours, I was writing it at the same time. @user63898 And if you really need to explicitly allocate an array of chars on the heap (rather than a string), don't use `new`, look into smart pointers with C++11 (or Boost before C++11). – MGA Jul 23 '14 at 11:40
  • @mga If you really need to explicitly allocate an array of `char` on the heap, use `std::vector`. (In pre-C++11, I did this a lot, because `std::string` didn't guarantee contiguity. In C++11, I'd just use `std::string` most of the time.) – James Kanze Jul 23 '14 at 11:47
  • @JamesKanze Agreed. I just wanted to point out never to use a raw `new`, as I suspect the OP comes from a Java/C# background where `new` is actually similar to smart pointers in C++ and not to `new`. But @user63898, yes, by all means, use `std::vector` or better still `std::string`. – MGA Jul 23 '14 at 11:54

1 Answers1

4

char represents a character (allocated on the stack). If you want to set it to empty, use char c = '\0' or char c = (char) 0.

char* cPtr is a pointer to a character. You can allocate an empty character on the heap with char* c = new char('\0').

char c[n] is a character array of size n.


Edit

As people have correctly pointed out below, a char is never empty, in the same sense as a container such as std::vector or std::string can be empty. A char is not fundamentally different to, say, an int, it's just shorter (1 byte as opposed to 2 or 4 or 8). Can an int be empty? Not as such; it can be zero, meaning that all its bits are set to zero in memory, and the same goes for a char. char c = '\0' will be represented as "00000000" on the stack.

A pointer to a char (char* cPtr), on the other hand can be 'empty' in the sense that it can point nowhere, by setting it to NULL. In this case, the pointer itself will exist on the stack and will contain a special sequence of 0/1's that your system interprets as NULL. Once you do cPtr = new char('\0'), a char (i.e. a byte) will be allocated on the heap and set to "00000000", and the value of cPtr on the stack will be changed to point to the address of the new character on the heap.

PS: don't actually do char* cPtr = new char('\0'), use an std::vector<char> or an std::string. Also, you may want to look into smart pointers.

Community
  • 1
  • 1
MGA
  • 1,658
  • 15
  • 28
  • The null terminator is not the same as "empty". – Oliver Charlesworth Jul 23 '14 at 11:12
  • In memory, the character's bytes will be initialized to zero. That's as close to "empty" as I can think of for a character. Of course a pointer can be set to NULL, but I don't think that's what the OP meant (but not sure). – MGA Jul 23 '14 at 11:14
  • 1
    A `char` is never empty. A string (regardless of its representation) can be empty, but a `char` is always exactly one character (or a part of one character, if e.g. you're using UTF-8). – James Kanze Jul 23 '14 at 11:39