-2

This is something I did as part of simple experimentation;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int fun()
{
 int d=10;
 write(1,(int*)d,sizeof(d));
}

int main()
{ 
 int x;
 fun();
 read(1,(int*)x,4);
 printf("x=%d\n",x);
 return 0;
}

As you can see I am trying to access value of d in fun() function via the IO stream (IO files) stdout; I tried using stdin as well but the thing is x value is not changing. (I tried in an online IDE.) But as per my thought stream, it should change to value of d.

Can anyone help me see why it is not doing that?

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
Febin Sunny
  • 311
  • 1
  • 13
  • Please show us an example of input and expected output. – Jabberwocky Feb 08 '17 at 07:09
  • Even if you fix up the calling sequences (pass pointers to `read()` and `write()`), you are not going to be able to read what `fun()` wrote. Yes, you often can read from standard output — but unless you've done something fancy (I/O redirection, etc), reading from standard output is the same as reading from standard input, meaning it will read what you type at the keyboard. What you wrote to standard output will go to the screen (unless you've done some I/O redirection) and will not become part of the input data. – Jonathan Leffler Feb 08 '17 at 07:28
  • @JonathanLeffler How to do that ? – Suraj Jain Feb 08 '17 at 07:32
  • @SurajJain: How to do what? – Jonathan Leffler Feb 08 '17 at 07:33
  • @SurajJain To `read()` what you did `write()` you need something where they are both connected to the same thing, for example a pipe, a socket or a file. So I suggest you `open` a file, `write` to it, `seek` back to the beginning and finally `read` what you have written. – Klas Lindbäck Feb 08 '17 at 08:22
  • I'm guessing you meant to use the **addresses** of `d` and `x`, rather than to cast them to pointers: `write(p[0],&d,sizeof d)` and `read(p[1], &x, sizeof x)`. Assuming that `p[]` has been populated using `pipe()`, of course. – Toby Speight Feb 08 '17 at 14:05

3 Answers3

3

read and write require pointers. In your case it is &d and &x. Casting int to int* usually makes no sense.

user31264
  • 6,557
  • 3
  • 26
  • 40
2

Replace line :

write(1,(int*)d,sizeof(d));

with :

write(1, &d, sizeof(d));

and line :

read(1,(int*)x,4);

with :

read(1, &x, 4);

Reason for this modification :

As you can see in this reference for read(), the prototype for function read is :

ssize_t read(int fd, void *buf, size_t count);

and respectively the prototype for function write is :

ssize_t write(int fd, const void *buf, size_t count); 

as shown in this reference for write().

Therefore, both functions require a pointer as their second argument. So you must pass a memory address to them.


NOTE : You try to make a pointer from an integer by casting it to int*. Actually, int* indicates a pointer in the case of a declaration or in a prototype, but when you use * in front of a variable's name, it indicates the contents of this variable. The symbol & is used to indicate the variable's address.

Marievi
  • 4,951
  • 1
  • 16
  • 33
2

First, correct call to read() and write() would include getting a pointer to your variable and passing it.

Second, you read and write from stdout (file descriptor 1), as opposed to your description. It doesn't matter if you read from stdout instead of stdin, according to https://stackoverflow.com/a/7680234/2543937

Third, it's better to use the unistd.h definitions (STDOUT_FILENO, STDIN_FILENO, etc), instead of fd numbers directly.

int d, x;
write(STDOUT_FILENO, &d, sizeof(d));
read(STDOUT_FILENO, &x, sizeof(x));
printf("x=%d\n", x);

In regard to what I think was your initial idea: when you read from stdout (or stdin), the data comes from the keyboard input, and not from what was printed to the screen earlier - so value of d should not make it to x.

Community
  • 1
  • 1
Alien_AV
  • 345
  • 1
  • 9