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

  • Home
  • SEARCH
  • 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 8039253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:30:52+00:00 2026-06-05T03:30:52+00:00

I have a C struct that basically contains two 2D char arrays called List.

  • 0

I have a C struct that basically contains two 2D char arrays called List. One for appended items and another for inserted items. Then use external functions that add C strings to these arrays called add_to_array.

The problem I’m having is when I call add_to_array once it goes through without issues but once called a second time, I get a segmentation fault. With test code, I discovered for what ever reason that I can’t figure out, the 2D array(s) in List remain NULL after calling add_to_array. I checked the result of add_to_array and it returns 1 (success) every time.

the target system/OS is Ubuntu linux.

typedef struct
{
  char** appended;
  char** inserted;
  size_t app_alloc;
  size_t app_elem;
  size_t ins_alloc;
  size_t ins_elem;
}
List;

void init_list(List* list)
{
  list->app_alloc = 0;
  list->ins_alloc = 0;
  list->app_elem = 0;
  list->ins_elem = 0;
  list->appended = NULL;
  list->inserted = NULL;
}

void free_list(List* list)
{
  size_t i = 0;

  for (; i < list->ins_elem; ++i)
  {
    free(list->inserted[i]);
  }
  free(list->inserted);

  i = 0;

  for (; i < list->app_elem; ++i)
  {
    free(list->appended[i]);
  }
  free(list->appended);
}


int add_to_array(const char* in, char** array, size_t* alloc, size_t* elem)
{
  if (*alloc == *elem)
  {
    if (*alloc == 0) *alloc = list_buff;
    else             *alloc = (*alloc) * 2;

    char** _tmp = (char**) realloc(array, (*alloc) * sizeof(char*));

    if (!_tmp) return 0;
    else       array = _tmp;
  }

  array[(*elem)] = (char*) malloc(strlen(in) + 1);

  strcpy(array[(*elem)], in);

  (*elem)++;

  return 1;
}

int append_list(const char* in, List* out)
{
  return add_to_array(in, out->appended, &out->app_alloc, &out->app_elem);
}

int insert_list(const char* in, List* out)
{
  return add_to_array(in, out->inserted, &out->ins_alloc, &out->ins_elem);
}

int main()
{
  List test;

  init_list(&test);

  append_list("test", &test);

  if (!test.appended)
  {
    printf("*%s*", "why is test.appended still NULL?");
  }

  //append_list("wwww", &test);
  //insert_list("ffff", &test);

  //printf("%s\n", get_element(0, &test));
  //printf("%s\n", get_element(1, &test));
  //printf("%s\n", get_element(2, &test));

  //free_list(&test);


  return 0;
}

Output: why is test.appended still NULL?

thanks to David’s advice, I got my code working here’s the changes:

int add_to_array(const char* in, char*** array, size_t* alloc, size_t* elem)
{
  if (*alloc == *elem)
  {
    if (*alloc == 0) *alloc = list_buff;
    else             *alloc = (*alloc) * 2;

    char** _tmp = (char**) realloc((*array), (*alloc) * sizeof(char*));

    if (!_tmp) return 0;
    else       (*array) = _tmp;
  }

  (*array)[(*elem)] = (char*) malloc(strlen(in) + 1);

  strcpy((*array)[(*elem)], in);

  (*elem)++;

  return 1;
}

int append_list(const char* in, List* out)
{
  return add_to_array(in, &out->appended, &out->app_alloc, &out->app_elem);
}

int insert_list(const char* in, List* out)
{
  return add_to_array(in, &out->inserted, &out->ins_alloc, &out->ins_elem);
}
  • 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-05T03:30:53+00:00Added an answer on June 5, 2026 at 3:30 am

    Because C is a pass-by-value language 🙂

    You seem to be expecting that when you call:

    add_to_array(in, out->appended, &out->app_alloc, &out->app_elem);
    

    and then do

    int add_to_array(const char* in, char** array, size_t* alloc, size_t* elem)
    {
    ....
    array = _tmp;
    ....
    

    that the change to array will also change out->appended.

    If you want it to work that way, you would have to pass a pointer to out->appended, and make add_to_array look like

    int add_to_array(const char* in, char*** array, size_t* alloc, size_t* elem)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a struct that contains local environments: public struct Environments {
I have a struct-array that contains details of different reports that can be run.
I have a struct array called AnalysisResults , that may contain any MATLAB datatypes,
I have an unmanaged struct I'd like to marshal to c# that looks basically
Basically, I have a struct which contains a number of fields. I'm going to
I have a struct that I am trying to use like an enum: public
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have a struct that I initialize like this: typedef struct { word w;
I have a struct that's defined like this: struct Vec3 { float x, y,
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];

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.