Falmarri
Falmarri

Reputation: 48577

Passing a constant integer when function expects a pointer

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?

Upvotes: 6

Views: 4404

Answers (7)

Northcode
Northcode

Reputation: 85

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.

Upvotes: 0

Notinlist
Notinlist

Reputation: 16640

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.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363607

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.

Upvotes: 3

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

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.

Upvotes: 10

Robᵩ
Robᵩ

Reputation: 168636

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

Upvotes: 3

user2100815
user2100815

Reputation:

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

Upvotes: 1

bmargulies
bmargulies

Reputation: 100051

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.

Upvotes: 1

Related Questions