简体   繁体   中英

How to remove these warning when erasing a c string

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.

   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)

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.

*foo = 0;

要“擦除”C样式字符串,您需要做的就是将第一个字节设置为0。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM