开发者

How to remove these warning when erasing a c string

开发者 https://www.devze.com 2023-01-01 11:24 出处:网络
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

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;

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号