警告:fopen() 调用

发布于 2024-11-28 20:01:27 字数 336 浏览 2 评论 0原文

你好,我正在 Linux 下使用 stdlib 进行编程。

gcc 对以下代码行发出以下警告,知道为什么吗?

FILE *fd;
if ( fd = fopen( filename, "rw" )== NULL )
{

警告是:

warning: assignment makes pointer from integer without a cast.

这是怎么发生的,根据 stdlib 文档,fopen 的返回类型是 FILE*。那么为什么仍然有警告?知道吗?

- 提前致谢 -

hi I'm programming with stdlib under linux.

The gcc emits the following warning for the following line of code, any idea why is that?

FILE *fd;
if ( fd = fopen( filename, "rw" )== NULL )
{

and the warning is:

warning: assignment makes pointer from integer without a cast.

How this can be happen , according to the stdlib documentation the return type of fopen is FILE*. So why there is a warning still there?Any idea?

--Thanks In Advance--

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的黑色迷你裙 2024-12-05 20:01:27

尝试

if ((fd = fopen( filename, "rw")) == NULL)
    ^                           ^ 

否则 fd 将采用值 0 或 1,并且 fopen 返回的 FILE * 本身将丢失。因此,如果没有这些括号,比较结果将存储在 fd 中,而不是存储在 FILE * 本身中。

Try

if ((fd = fopen( filename, "rw")) == NULL)
    ^                           ^ 

Otherwise fd will take the value 0 or 1 and the FILE * itself returned by fopen will be lost. So without those parentheses the result of the comparison will be stored in fd instead of the FILE * itself.

旧夏天 2024-12-05 20:01:27

您实质上是将 fd 指定为 fopen(filename, "rw") == NULL,因为条件表达式是一个整数(0 或 1),您正在指定一个来自整数的指针。按照 @cnicutar 的答案进行修复

You are essentially assigning fd to be fopen(filename, "rw") == NULL, as a conditional expression is an integer (0 or 1), you are assigning a pointer from an integer. follow @cnicutar's answer for the fix

幽梦紫曦~ 2024-12-05 20:01:27
FILE *fd;
if ( (fd = fopen( filename, "rw" ))== NULL )
{

只需复制此代码即可工作......
你只是忘了把 () 放在 "fd = fopen( filename, "rw" )" 周围

FILE *fd;
if ( (fd = fopen( filename, "rw" ))== NULL )
{

just copy this code it will work....
you just forget to put () arround "fd = fopen( filename, "rw" )"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文