开发者

Calloc simple problem

开发者 https://www.devze.com 2023-04-04 21:54 出处:网络
char* lw; if((lw=(char *)calloc(2, sizeof(char))==0)) printf(\"Failed to allocate.\\n\"); else printf(\"a开发者_开发问答llocated %p\\n\", lw);
char* lw;
if((lw=(char *)calloc(2, sizeof(char))==0)) 
   printf("Failed to allocate.\n"); 
else 
   printf("a开发者_开发问答llocated %p\n", lw);

I've read manual for calloc, need a quick and brief answer, why does it output NIL? It goes to else, so lw cannot possibly be NIL, then outputs "allocated (nil)"? God's hand? ;)


Because you are assigning to lw calloc(...) == 0, which is false. You want it the other way around

Replace ==0)) with )==NULL).

0

精彩评论

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

关注公众号