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

Edited 3 Years Ago by happygeek: fixed formatting

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. http://public.research.att.com/~bs/bs_faq2.html

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

This article has been dead for over six months. Start a new discussion instead.