Passing a constant integer when function expects a pointer

…衆ロ難τιáo~ 提交于 2020-01-03 13:08:09

问题


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?


回答1:


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.




回答2:


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);



回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/6116982/passing-a-constant-integer-when-function-expects-a-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!