I create the dynamic array with malloc and assign the last char to NULL. When I assign the NULL character, the warning message as follow appear:

'=' : 'unsigned char ' differs in levels of indirection from 'void *'

The code is as follows:

void main()
{
             const char sendText[] = "Hi";
             buffersize = strlen( sendText );
    buffer = (unsigned char*)(malloc(  buffersize + 1 ));   memcpy( buffer, sendText, buffersize );
    buffer[buffersize] = NULL;
}

Thanks a lot.

Rgds,
perlsu

Dani AI

Generated

Quick answer: the warning comes from assigning a pointer-like macro to a character slot. NULL is a null pointer constant on many systems (sometimes defined as (void*)0), so buffer[buffersize] = NULL; tries to store a pointer into an unsigned char and the compiler complains. Use a character-zero ('\0') or the integer 0 to terminate a string, not NULL. explained the distinction well and ’s point about NULL’s definition is also relevant.

A few practical improvements and checks:

  • Use int main() instead of void main().
  • Keep lengths as size_t and always allocate len + 1 for the terminator. Check allocation success before using the buffer.
  • If you expect text, prefer higher-level types (safe-by-default) in C++: std::string, or std::vector<unsigned char> if you need raw bytes. If you must manage memory manually, prefer RAII (std::unique_ptr<unsigned char[]>) or at least remember to free() the buffer.
  • If the data is binary, do not rely on a NUL terminator — track the byte count instead.

Example (modern C++ approach — avoids raw malloc and the NULL issue):

#include <vector>
#include <string>

std::string s = "Hi";
std::vector<unsigned char> buf(s.begin(), s.end()); // copies characters
buf.push_back('\0'); // add terminator only if you need C-style access

If sticking with manual allocation in C/C++, use a real character zero when terminating and prefer memcpy with an explicit length or safe copy routines (or strncpy/strlcpy where appropriate). If warnings persist, inspect how your compiler defines NULL (or use nullptr in modern C++ for pointer literals).

Recommended Answers

All 4 Replies

why are you using memcpy()? use strcpy() and you won't have to worry about null-terminating the string.

void main()
{
const char sendText[] = "Hi";
buffersize = strlen( sendText );
buffer = (unsigned char*)malloc( buffersize + 1 );
strcpy( buffer, sendText);

}

>>'=' : 'unsigned char ' differs in levels of indirection from 'void *'

you should not use NULL. The macro NULL can be defined as anything, and some compilers define it as (void *)0. I believe C++ now requires it to be just 0.

buffer[buffersize] = 0;

>When I assign the NULL character
NULL isn't a character, it's a macro representing a null pointer. This is a casualty of the multiple meanings for the word "null". Use '\0' for a null character and NULL for a null pointer, or 0 for both if you want to be cryptic.

>The macro NULL can be defined as anything
Anything that evaluates to a null pointer constant. That somewhat limits the meaning of "anything". ;) But ((void)*0) is among those options in C.

>I believe C++ now requires it to be just 0.
Correct. If C++ used the old C-style ((void *)0), a cast would be required every time you used NULL. Since 0 is a null pointer of every type, no cast is needed and that makes life easier.

>>Anything that evaluates to a null pointer constant

What I really meant was that since NULL is a macro you can redefine it as anything you want it to be

#if defined(NULL)
#undef NULL
#define NULL "Hello World"
#endif

I haven't seen anyone do that, but I suppose its possible. And that is why Bjarne Stroustrup said he doesn't use that macro, he always uses 0 instead, as shown in my post.

>since NULL is a macro you can redefine it as anything you want it to be
It's a standard macro without a defined function replacement, so if you redefine it, you deserve exactly what you get. I could apply your argument to anything in the standard library simply by overriding the definition with something of my own. Therefore, malloc could do anything, strtol could to anything, struct tm could be anything, etc...

Your logic is flawed. NULL is what the standard says it is. If you redefine it, that's your problem, and it has no place in a general description of the macro.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.