Writing integers to file descriptor using write?

11,204

Solution 1

You need to pass an address, so you'll need a variable of some form or other.

If all you need is a single char:

char c = 1;
write(fd, &c, 1);
c = 0x35;
write(fd, &c, 1);

Or use an array (this is generally more common):

char data[2] = { 0x01, 0x35 };
write(fd, data, 2);

Solution 2

The second parameter should be a pointer to a buffer, you can do that:

char a = 1;
write(fd, &a, 1);

or even simpler:

char buff[] = {1, 0x35};
write(fd, buff, sizeof(buff));
Share:
11,204
Kobi
Author by

Kobi

Hobbyist programmer from Japan!

Updated on June 28, 2022

Comments

  • Kobi
    Kobi almost 2 years

    I want to write the integer 1 into the first byte and 0x35 to the second byte of a file descriptor using write (http://linux.about.com/library/cmd/blcmdl2_write.htm) but I get the following warning when I try the following:

    write(fd, 1, 1);
    write(fd, 0x35, 1);
    
    
    source.c:29: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast
    source.c:30: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast