0

this code works:

void main (void)
{
    write (1, "1", 1);
}

this one works too:

void putchar(char c)
{
    write (1, &c, 1);   
}
void main (void)
{
    putchar('1');
}

but this one doesn’t :

void main (void)
{
    write (1, '2', 1);
}

in the second code, c is a character (not a pointer), so why is there & before c ?

sina ardehali
  • 19
  • 1
  • 5
  • In the second example `&c` is a pointer to `c`. – Ian Abbott Oct 03 '16 at 12:37
  • 1
    The third example does not work because you used a character value instead of a pointer. C will convert the character value to a pointer the best it can, and should give a warning, but the pointer value will be invalid. – Ian Abbott Oct 03 '16 at 12:38
  • The second argument expected by write funtion should be a pointer. '2' is not a pointer. see [here](http://codewiki.wikidot.com/c:system-calls:write) – P.Bra Oct 03 '16 at 12:38

1 Answers1

0

Probably your write function looks like

write(int a, char *string, int b)

So it expects pointer as a second argument. First and the second case fits it but the last one passes char (not pointer to char) as second argument

EDIT:

As mch wrote the prototype is ssize_t write(int fildes, const void *buf, size_t nbyte); so the second argument must be a pointer, not char

Notice: "A" is treated like array of chars {'A', '\0'}, &c is address of variable of type char (so it still fits to function which expects pointer to char) but 'A' is a single character so it does not fit.

gravell
  • 158
  • 1
  • 12
  • 1
    The prototype is `ssize_t write(int fildes, const void *buf, size_t nbyte);`. – mch Oct 03 '16 at 12:39
  • As you see it expects pointer as a second argument. In first case you pass pointer to array of characters, in the second you pass pointer to char but in the third one you pass char (not pointer to it) so it does not fit to `write` function. – gravell Oct 03 '16 at 12:42
  • This is the [write](http://pubs.opengroup.org/onlinepubs/7908799/xsh/write.html). – theVoid Oct 03 '16 at 12:42
  • `'2'` is an `int`, not a `char` in C. – mch Oct 03 '16 at 12:47
  • But i don't agree with you. in the second i am given a character not a pointer. look : putchar('1'). so in the second case, i am given the character ascii 1. am i wrong? – sina ardehali Oct 03 '16 at 12:49
  • @mch: '2' is char, not an int (int a = 2; char b = '2';) – gravell Oct 03 '16 at 12:50
  • @Gravell i understand what you are saying for the first case, but the second and the third case are exactly the same. in both case, i am giving to write a number between single quotes : '1' for the second case and '2' for the third case. – sina ardehali Oct 03 '16 at 12:55
  • @sina ardehali: the second and the third one are not the same. There is a big difference between passing an address to char and passing a char. You `putchar` function takes `char` as an argument, `write` function takes pointer to void (so it may also be a pointer to char) but it's still pointer – gravell Oct 03 '16 at 12:59
  • @Gravell thank you very much , it makes more sense for me, but i still need to see these notions. can you recommend me some links or places on internet where i can find much about these notions? – sina ardehali Oct 03 '16 at 13:04
  • @sina ardehali I would recommend a book describing C language - https://en.wikipedia.org/wiki/The_C_Programming_Language (pointers explained in easy way). Also you may look up for "pointer tutorials in C" - you should find at least few interesting ones. – gravell Oct 03 '16 at 13:10
  • @Gravell thank you very much. it really helped. Wish you the best! – sina ardehali Oct 03 '16 at 13:13