1

I have some problems with a pointer. My idea was to pass a buffer to a function in order to store the return data in this buffer. But, I do not know if it will work.

void main()
{
    char *buf = malloc(sizeof(char) *buf);
    memset(buf, 0x00, BUF_SIZE);
    sendCommand(buf);
}

sendCommand(char *buf)
{
    write(fd, "some commands", strlen("some commands"));
    readResponse(buf);
}

readResponse(char *buf)
{
    read(fd, buf, nbytes);
}

I know there is no error handling up to now. And some variables are not well defined. It just depends on the passing buffer. Will I see the data that I get in readResponse() in my main function?

7
  • 5
    It's int main(void); you are not free to declare main as you please. Commented Sep 15, 2015 at 13:50
  • Have you tested it ? Commented Sep 15, 2015 at 13:50
  • If you are going to do memset(buf, 0x00, BUF_SIZE);, you need char *buf = malloc(BUF_SIZE);. Your expression sizeof(char) *buf doesn't make sense. Did you mean BUF_SIZE * sizeof(*buf)? Commented Sep 15, 2015 at 13:50
  • Sidenote: use clear names describing what the function does. sendCommand should either just send the command, not get the response, or you change the name to something more appropriate. Commented Sep 15, 2015 at 13:51
  • Yes, if you pass a pointer as a function argument, the function can modify the pointed-to object. That is how scanf() works, for example. And so, too, the very same read() function you are using, for that matter. Commented Sep 15, 2015 at 13:51

3 Answers 3

3

As in readResponse() as you read nbytes into buffer pointed by buf ,so you will get that data in main .

Some improvements to be done -

1. void main() -> int main(void) or int main(int argc, char *argv[])

2. char *buf = malloc(sizeof(char) *buf); -> char *buf = malloc(BUF_SIZE); // sizeof(char)=1 or maybe something you desire (not sure though what you want ??)

Note - remember to free allocated memory.

Sign up to request clarification or add additional context in comments.

Comments

1

You have a remarkable number of significant problems in the code you presented, considering how short it is. Other answers have addressed those, though, and your question is not actually about any of them:

Will I see the data I get in readResponse() in my main function?

Yes, provided that argument buf is a pointer to an array large enough to accommodate nbytes bytes, and that the read() call in fact successfully reads any bytes, those bytes will afterward be visible in main() via the pointer it passed to readResponse(). More generally, if you pass a pointer as a function argument, the called function may manipulate the pointed-to object, including by modifying those parts of it that are not const. That's how the read() function itself is able to store the bytes it reads into your buffer, after all.

Comments

0

This won't do what you think it does:

char *buf = malloc(sizeof(char) *buf);

Did you mean to multiply with BUF_SIZE?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.