11

I have this block of code:

int myFunc( std::string &value )
{
    char buffer[fileSize];
    ....
    buffer[bytesRead] = NULL;
    value = buffer;
    return 0;
}

The line - buffer[bytes] = NULL is giving me a warning: converting to non-pointer type 'char' from NULL. How do I get rid of this warning?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Owen
  • 4,063
  • 17
  • 58
  • 78

3 Answers3

30

Don't use NULL? It's generally reserved for pointers, and you don't have a pointer, only a simple char. Just use \0 (null-terminator) or a simple 0.

Xeo
  • 129,499
  • 52
  • 291
  • 397
3

buffer[bytesRead] = 0; // NULL is meant for pointers

As a suggestion, if you want to avoid copying and all then, below can be considered.

int myFunc (std::string &value)
{
  s.resize(fileSize);
  char *buffer = const_cast<char*>(s.c_str());
  //...
  value[bytesRead] = 0;
  return 0;
}
iammilind
  • 68,093
  • 33
  • 169
  • 336
0

NULLNUL.

NULL is a constant representing a null pointer in C and C++.

NUL is the ASCII NUL character, which in C and C++ terminates strings and is represented as \0.

You can also use 0, which is exactly the same as \0, since in C character literals have int type. In C++, character constants are type char.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • 1
    The question is falsly tagged as `c`, since the code clearly uses a `std::string`, which is C++. As such, string literals aren't `int` but correctly `char`. – Xeo May 18 '11 at 06:49
  • From your answer I atleast can see how the OP might have gotten to `NULL` for initializing the char.. thanks for that. xD – Xeo May 18 '11 at 06:50