当函数需要指针时传递常量整数
将常量整数值传递给需要指针的函数的最佳/最规范的方法是什么?
例如, write 函数
write (int filedes, const void *buffer, size_t size);
假设我只想写一个字节(a 1),我会这样想:
write (fd, 1, 1);
但我显然收到了警告
warning: passing argument 2 of 'write' makes pointer from integer without a cast
,我知道我可以这样做
int i = 1;
write (fd, &i, 1);
,但这有必要吗?无需声明/初始化新变量的最正确方法是什么?
What's the best/most cannonical way of passing in a constant integer value to a function that expects a pointer?
For example, the write
function
write (int filedes, const void *buffer, size_t size);
Let's say I just want to write a single byte (a 1), I would think this:
write (fd, 1, 1);
but I obviously get the warning
warning: passing argument 2 of 'write' makes pointer from integer without a cast
I know I can do
int i = 1;
write (fd, &i, 1);
but is that necessary? What's the most correct way of doing this without the need of declaring/initializing a new variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 C89/90 中,如果不声明新变量,通常无法做到这一点。 C 中指针的特点是它们总是指向左值。左值是在内存中有一个位置的东西。因此,为了向您的函数提供正确的参数,您需要在内存中拥有一个位置的东西。这通常需要一个定义,即一个对象、一个变量。 C 中的常量不是左值,因此不能在那里使用常量。介于常量和变量之间的东西是文字。 C89/90 只有一种文字:字符串文字。字符串文字是左值,但不是变量。因此,您可以使用字符串文字作为 write 的第二个参数。
在 C99 中,文字的概念已扩展到字符串文字之外。在 C99 中,您还可以使用复合文字来实现此目的,这也恰好是左值。在您的示例中,您可以将函数调用为
但此功能仅在 C99 中可用。
In C89/90 you can't generally do it without declaring a new variable. The thing about pointers in C is that they always point to lvalues. Lvalue is something that has a location in memory. So, in order to supply the proper argument to your function, you need something that has a location in memory. That normally requires a definition, i.e. an object, a variable. Constants in C are not lvalues, so you can't use a constant there. Things that lie somewhat in between constants and variables are literals. C89/90 has only one kind of literal: string literal. String literals are lvalues, but not variables. So, you can use a string literal as the second argument of
write
.In C99 the notion of literal was extended beyond string literals. In C99 you can also use compound literals for that purpose, which also happen to be lvalues. In your example you can call your function as
But this feature is only available in C99.
对于您引用的具体案例,您可以这样做:
但是,更一般地说,您必须做您所抱怨的事情。您必须在获取对象的地址之前声明该对象:
For the specific case you cite, you can do this:
But, more generally, you must do that about which you are complaining. You must declare an object before taking its address:
是的,那是必要的。在 C 中没有其他同样清晰的方法可以做到这一点,因为您无法获取临时变量的地址/获取临时变量的指针。
(并且,正如 @rob 已经说过的,您应该获取
char
的地址。您的示例是不可移植的。)编辑:请参阅 @AndreyT 的答案并设置您的编译器到 C99 模式。
Yes, that's necessary. There's no other way to do this in C that is equally clear, since you can't take the address of/get a pointer to a temporary.
(And, as @rob already said, you should take the address of a
char
. Your example is non-portable.)EDIT: See @AndreyT's answer and set your compiler to C99 mode.
整数不是指针。如果你给它传递一个 1,它将取消引用虚拟地址 1 并且死亡。您必须创建该变量并传递其地址。
An integer isn't a pointer. If you pass it a one, it will dereference virtual address 1 and die. You must make that variable and pass its address.
写入需要一个地址作为第二个参数 - 接受它。
Write requires an address as is second parameter - live with it.
您可以使用
drprintf()
打印到文件描述符。它类似于打印到FILE *
的fprintf()
。有关详细信息,请参阅
man 3 dprintf
。函数 dprintf() 和 vdprintf()(在 glibc2 库中找到)与 fprintf(3) 和 vfprintf(3) 完全相同,只不过它们输出到文件描述符 fd 而不是 stdio 流。
它符合 POSIX.1,但不通用。
You can use
drprintf()
which prints to a file descriptor. It is similar tofprintf()
which prints to aFILE *
.See
man 3 dprintf
for details.The functions dprintf() and vdprintf() (as found in the glibc2 library) are exact analogs of fprintf(3) and vfprintf(3), except that they output to a file descriptor fd instead of to a stdio stream.
It is POSIX.1 compliant, not universal.
这似乎有效。不知道这有多好,因为我没有太多使用 C++。
让我知道这是否是一个不好的方法。
This seems to work. Not sure how good this is as I haven't used c++ that much.
Let me know if this is a bad way to do this.