Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get rid of - "warning: converting to non-pointer type 'char' from NULL"?

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?

like image 525
Owen Avatar asked May 18 '11 05:05

Owen


2 Answers

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.

like image 186
Xeo Avatar answered Nov 15 '22 08:11

Xeo


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;
}
like image 34
iammilind Avatar answered Nov 15 '22 06:11

iammilind