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 8234865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:37:33+00:00 2026-06-07T18:37:33+00:00

How do I access elements in this stuct if I initialize a list as

  • 0

How do I access elements in this stuct if I initialize a list as follows:

group **list = (group **) malloc(sizeof(group)); 

typedef struct
{
    // ID of the group, 'A' to 'D'
    char id;

    // a list of members in the group 
    char **members;
} group;

I tried using (*list)->id = 'A' and it compiles but then gets a segmentation fault error when running the program.

  • 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-07T18:37:34+00:00Added an answer on June 7, 2026 at 6:37 pm

    Although it compiles fine, you didn’t allocate memory correctly.

    You allocated memory for group **list, which is eventually an array of pointers to struct group. What I think you intended to do doing is:

    group** list = malloc(sizeof(group*) * 5);  // e.g. 5 pointers
    

    Now for each pointer in the array, allocate its own memory:

    int i;
    for (i = 0; i < 5; i++) {
        list[i] = malloc(sizeof(group));
    }
    

    For instance, to access id in the 2nd struct, you do:

    list[1]->id = 'A';
    

    Note that *list access the first struct, and is equivalent to list[0].

    Sidenote:
    Two levels of indirection allow you to store the structs in a non-continuous way in the memory. Alternatively, you can use one level of indirection and store them continuously:

    group list* = malloc(sizeof(group) * 5);  // Again, 5 structs
    

    and then access the members with:

    list[0].id = 'A';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to access the text elements inside this textbox in GWT from the
i cant access clicked object(this) parent's class.. click same elements and different returns? this
I need to access the HashMap key elements much like a linked list with
I have an array that looks like this: struct table_elt { int id; char
This question is a continuation of Malloc call crashing, but works elsewhere I tried
I have this complicated structure thingie: #include <stdlib.h> typedef struct { int x; int
I have a dictionary that goes like this: typedef struct dictNode { int key;
I can't access a element with its href. Like this example, i need add
I am attempting to access elements with a specific class name from a page
I use an array to store fieldvalues. To easily add and access elements I

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.