Reputation: 227
I have a struct defined as
struct my_struct {
struct hdr_str hdr;
char *content;
};
I am trying to pass the content in my_struct of the first element in my_struct by nesting it all as a parameter to read()
what I have is
struct my_struct[5];
read is defined as
ssize_t read(int fd, void *buf, size_t count);
and I am trying to pass it as
read(fd, my_struct[0].content, count)
But I am receiving -1 as a return value, with errno = EFAULT (bad address)
Any idea how to make read read into a char * in a struct array?
Upvotes: 0
Views: 71
Reputation: 42215
You'll need to allocate memory for read
to copy data into.
If you know the maximum size of data you'll read, you could change my_struct to
struct my_struct {
struct hdr_str hdr;
char content[MAX_CONTENT_LENGTH];
};
where MAX_CONTENT_LENGTH
is #define'd by you to your known max length.
Alternatively, allocate my_struct.content
on demand once you know how many bytes to read
my_struct.content = malloc(count);
read(fd, my_struct[0].content, count);
If you do this, make sure to later use free
on my_struct.content to return its memory to the system.
Upvotes: 2