- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 open_memstream
为嵌入式 linux 系统编写一些 C 代码,但我不明白为什么会收到编译警告:assignment makes pointer from integer without a cast
为了简单起见,我没有粘贴我所有的代码,而是用 here 中的小示例重现了问题。 :
#include <stdio.h>
#include <stdlib.h>
int
main (void)
{
FILE *stream;
char *buf;
size_t len;
off_t eob;
stream = open_memstream (&buf, &len);
if (stream == NULL)
/* handle error */ ;
fprintf (stream, "hello my world");
fflush (stream);
printf ("buf=%s, len=%zu\n", buf, len);
eob = ftello(stream);
fseeko (stream, 0, SEEK_SET);
fprintf (stream, "good-bye");
fseeko (stream, eob, SEEK_SET);
fclose (stream);
printf ("buf=%s, len=%zu\n", buf, len);
free (buf);
return 0;
}
代码有效,但编译器提示行 stream = open_memstream (&buf, &len);
它指的是什么整数?我们按照函数原型(prototype)的要求传入一个指向 size_t 的指针。
FILE *open_memstream(char **bufp, size_t *sizep);
这段代码有问题吗,还是我需要看看我的编译器?我想以正确的方式摆脱这个警告。
更新:
使用 gcc 4.3.2、glibc 2.9
更新 2:
尝试了以下方法:
powerpc-860-linux-gnu-gcc -std=c99 -Wall -D_XOPEN_SOURCE=700 -c source.c
结果:
source.c: In function 'main':
source.c:12: warning: implicit declaration of function 'open_memstream'
source.c:12: warning: assignment makes pointer from integer without a cast
根据 this ,看来 _XOPEN_SOURCE=700 可用自 glibc 2.10。
由于我使用的是 glibc 2.9,我还有哪些其他选择(除了升级 glibc)?
更新 3:
添加以下内容消除了警告:
extern FILE *open_memstream(char **bufp, size_t *sizep);
这个方案有什么问题吗?
更新 4:
这代替了外部:
powerpc-860-linux-gnu-gcc -std=c99 -Wall -D_GNU_SOURCE -c ops_cmds.c
所以根据manpage ,需要使用 _GNU_SOURCE
如果 glibc pre-2.10(在我的例子中)和 _XOPEN_SOURCE=700
如果 2.10+
最佳答案
定义:
#define _POSIX_C_SOURCE 200809L
或
#define _XOPEN_SOURCE 700
在包含 stdio.h
之前的源代码中。或者使用 gcc
,您可以使用 -D
选项定义宏值并将其传递给源文件:
gcc -std=c99 -Wall -D_XOPEN_SOURCE=700 -c source.c
open_memstream
是一个 POSIX 函数,如果没有这个定义,它的声明在您的程序中是不可见的。
关于c - open_memstream 警告 "pointer from integer without a cast",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14862513/
我通过 open_memstream() 创建一个流,填充它然后读取。它工作正常。但我很困惑函数文档说:“open_memstream() 函数打开一个流以写入缓冲区”。但我也能读懂。我是否以正确的方
全部, 我有一个打印到流的程序。我需要在内存中缓冲此流,然后根据需要将每一行打印到实际文件中。 因为 fprintf() 函数调用必须有一个 FILE * 指针,所以我需要在内存中指出指针寻址空间。我
我在我的库中使用 open_memstream,但我想将此库移植到 MSVC。似乎没有可用的等效功能,但是是否有足够相似的功能? open_memstream 的作用是获取一个 char** 目标和大
我正在尝试在我的 C 代码中使用 open_memstream 函数。但是我似乎无法编译它。最小工作示例如下: #include int main(void) { char *buf;
在阅读 string streams 上的 GNU 文档时我发现了两个相似的函数,它们做的事情非常相似: FILE * fmemopen (void *buf, size_t size, const
我使用一些 C 代码将二进制数据写入文件。在此过程中,它会四处寻找不同的位置,然后最终使用 fseeko(fp, 0, SEEK_END); 寻找结束。 但是,在某些情况下,我想改为处理内存中的流。为
我正在使用 open_memstream 为嵌入式 linux 系统编写一些 C 代码,但我不明白为什么会收到编译警告:assignment makes pointer from integer wi
我是一名优秀的程序员,十分优秀!