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?
You’ll need to allocate memory for
readto copy data into.If you know the maximum size of data you’ll read, you could change my_struct to
where
MAX_CONTENT_LENGTHis #define’d by you to your known max length.Alternatively, allocate
my_struct.contenton demand once you know how many bytes to readIf you do this, make sure to later use
freeon my_struct.content to return its memory to the system.