tags:

views:

86

answers:

3

How can I remove these warnings?

   char foo[10], msg2[100]; 
   int k;
   for (k = 0; foo[k] != NULL; k++) //comparison between pointer and integer
       msg2[k] = NULL; //assignment makes integer from pointer without a cast

Thanks.

+2  A: 
   int k;
   for (k = 0; foo[k] != '\0'; k++)
       msg2[k] = '\0';

Assign an integer to an integer variable, instead of a pointer. NULL is a pointer. It is usually defined as:

((void *) 0)
WhirlWind
... and compare foo[k] to 0
Autopulated
@Autopulated yeah, I was just editing it. thanks.
WhirlWind
`msg2[k] = '\0';` is generally preferred over `msg2[k] = 0;`, although both are correct of course.
Paul R
@Paul fair enough, though it wasn't included in the question when I wrote the answer.
WhirlWind
+1  A: 

The warning is right, your usage of NULL is wrong.

NULL was meant for pointers, not for string nul-terminator.

Use zero instead of NULL in your code, in BOTH places. If you want a special zero, you may use '\0', but that;s redundant.

Pavel Radzivilovsky
+1  A: 
*foo = 0;

To "Erase" a C style string, all you need to do is set the first byte to 0.

dicroce
And if you want to fill ablock of memory with 0s, use memset
Arkadiy
I believe using `'\0'` instead of `0` makes it more obvious that we're dealing with a char value.
ereOn