,但我想知道我是否应该改为: 断言(0!= p); In C89, do we have to pass an int as an argument to assert? I''ve got codeat the moment that does an assertion on pointer, e.g.: assert(p);, but I''m wondering if I should change that to: assert(0 != p); 是的,在C89中原型用于int。在C99中,这已经被改为 到任何标量表达式(当然你不能把它写成 原型,但那是实现'的问题,不是你好)。 - Ben。 Yes, in C89 the prototype is for an int. In C99 this has been changedto any scalar expression (of course you can''t write that as aprototype but that is the implementation''s problem, not yours). --Ben. Tomásóhéilidhe说: Tomás ó héilidhe said: > 在C89中,我们是否必须将int作为参数传递给断言?我在代码断言时有代码 ,例如: assert(p); >In C89, do we have to pass an int as an argument to assert? I''ve got codeat the moment that does an assertion on pointer, e.g.: assert(p); 4.2.1.1断言宏 概要 #include< ; assert.h> void assert(int expression); 所以你必须提供一个int。如果你不这样做,行为是不明确的。 (你似乎已经猜到了,在C99中这已经改变了,而且只有表达 必须是标量 - 对int的限制已被解除。) 4.2.1.1 The assert macro Synopsis #include <assert.h>void assert(int expression); So you have to provide an int. If you don''t, the behaviour is undefined. (As you seem to have guessed, in C99 this has changed, and the expressiononly has to be scalar - the restriction to int has been lifted.) > ,但我想知道我是否应该改为: 断言(0!= p); >, but I''m wondering if I should change that to: assert(0 != p); 是的,你应该(或使用等效的断言(p!= 0)或断言(p!= NULL)等等。 - Richard Heathfield< http://www.cpax.org.uk> 电子邮件:-http:// www。 + rjh @ 谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php> Usenet是一个奇怪的放置" - dmr 1999年7月29日 Yes, you should (or use an equivalent such as assert(p != 0) or assert(p !=NULL) etc). --Richard Heathfield <http://www.cpax.org.uk>Email: -http://www. +rjh@Google users: <http://www.cpax.org.uk/prg/writings/googly.php>"Usenet is a strange place" - dmr 29 July 1999 Richard Heathfield< rj*@see.sig.invalidwrote在 新闻:c9 ****** ************************@bt.com: Richard Heathfield <rj*@see.sig.invalidwrote innews:c9******************************@bt.com: >,但我想知道我是否应该改变它: 断言(0!= p); >, but I''m wondering if I should change that to: assert(0 != p); 是的,您应该(或使用等效的断言(p!= 0)或 断言(p!= NULL)等等)。 Yes, you should (or use an equivalent such as assert(p != 0) orassert(p != NULL) etc). 感谢您的信息。我最近刚刚更改了 编译器时遇到了这个问题,我收到了一些关于转换的警告,从 指针到整数类型:-O 看起来我会做一个在文件中查找用于断言 :-P (我不禁想到我以前的编译器实际上在做什么......是吧 将指针作为标量或是它在做什么东西......脏......) - $ b $bTomásóhéilidhe Thanks for the info. I only came across the problem when I changedcompilers recently, I got a boat-load of warnings about conversions frompointers to integer type :-O Looks like I''ll be doing a "Find in files" for "assert" :-P (I shudder to think what my previous compiler was actually doing... was ittaking the pointer as a scalar or was it doing something... dirty... )--Tomás ó héilidhe 这篇关于断言必须采取int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 17:53