Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:29:00+00:00 2026-06-13T10:29:00+00:00

I have a struct defined as struct my_struct { struct hdr_str hdr; char *content;

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:29:01+00:00Added an answer on June 13, 2026 at 10:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct defined as: struct { char name[32]; int size; int start;
I have a struct where a constant char string is defined and a pointer
I have a struct defined as struct _element; typedef struct _element Element; struct _element
I have a struct that's defined like this: struct Vec3 { float x, y,
I have defined a struct ABC to contain an int ID, string NAME, string
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have defined the following struct in C: typedef struct point{ float x; float
I have defined a custom struct which I need to send over to another
I have an adjacency_list graph defined as follows: struct VertexProperties{ std::string name; ... };
I have a structure defined like so: typedef struct { int n; int *n_p;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.