当函数需要指针时传递常量整数

发布于 2024-11-09 05:04:48 字数 461 浏览 0 评论 0原文

将常量整数值传递给需要指针的函数的最佳/最规范的方法是什么?

例如, 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 技术交流群。

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

发布评论

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

评论(7

灼痛 2024-11-16 05:04:48

在 C89/90 中,如果不声明新变量,通常无法做到这一点。 C 中指针的特点是它们总是指向左值。左值是在内存中有一个位置的东西。因此,为了向您的函数提供正确的参数,您需要在内存中拥有一个位置的东西。这通常需要一个定义,即一个对象、一个变量。 C 中的常量不是左值,因此不能在那里使用常量。介于常量和变量之间的东西是文字。 C89/90 只有一种文字:字符串文字。字符串文字是左值,但不是变量。因此,您可以使用字符串文字作为 write 的第二个参数。

在 C99 中,文字的概念已扩展到字符串文字之外。在 C99 中,您还可以使用复合文字来实现此目的,这也恰好是左值。在您的示例中,您可以将函数调用为

write(fd, (char[]) { 1 }, 1)

但此功能仅在 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

write(fd, (char[]) { 1 }, 1)

But this feature is only available in C99.

吹泡泡o 2024-11-16 05:04:48

对于您引用的具体案例,您可以这样做:

write(fd, "\001", 1);

但是,更一般地说,您必须做您所抱怨的事情。您必须在获取对象的地址之前声明该对象:

 SomeType i;
 SomeFUnction(&i);

For the specific case you cite, you can do this:

write(fd, "\001", 1);

But, more generally, you must do that about which you are complaining. You must declare an object before taking its address:

 SomeType i;
 SomeFUnction(&i);
醉生梦死 2024-11-16 05:04:48

是的,那是必要的。在 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.

苯莒 2024-11-16 05:04:48

整数不是指针。如果你给它传递一个 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.

原谅过去的我 2024-11-16 05:04:48

写入需要一个地址作为第二个参数 - 接受它。

Write requires an address as is second parameter - live with it.

淡墨 2024-11-16 05:04:48

您可以使用 drprintf() 打印到文件描述符。它类似于打印到 FILE *fprintf()

int dprintf(int fd, const char *format, ...);

有关详细信息,请参阅 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 to fprintf() which prints to a FILE *.

int dprintf(int fd, const char *format, ...);

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.

夕嗳→ 2024-11-16 05:04:48
write(var, new int(4))

这似乎有效。不知道这有多好,因为我没有太多使用 C++。
让我知道这是否是一个不好的方法。

write(var, new int(4))

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.

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