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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:13:45+00:00 2026-05-27T10:13:45+00:00

Have to create an array of struct. Since don’t know how many entries will

  • 0

Have to create an array of struct. Since don’t know how many entries will have in the array, have to use dynamic array.

Did not worked as advised here – https://stackoverflow.com/a/8455125/1090944.

Having troubles with passing array of struct to function for allocation.

Here is the code:

#include<stdio.h>
#include<stdlib.h>


struct record
{
    char * community_name;
    double data[10];
    double crimes_per_pop;
};

void allocate_struct_array(struct record * array, int length);

int main()
{
    int num_lines = 10;
    struct record ** data_array;

    allocate_struct_array(&data_array, num_lines);

    data_array[0]->community_name[0] = 'h';
    printf("%c\n", data_array[0]->community_name[0]);

    return 0;
}


void allocate_struct_array(struct record * array, int length)
{
    int i;

    *array = malloc(length * sizeof(struct record *));

    if (!array)
    {
        fprintf(stderr, "Could not allocate the array of struct record *\n");
        exit(1);
    }

    for (i = 0; i < length; i++)
    {
        array[i] = malloc( sizeof(struct record) );

        if (!array[i])
        {
            fprintf(stderr, "Could not allocate array[%d]\n", i);
            exit(1);
        }

        array[i]->community_name = malloc(100 * sizeof(char));
    }
}

Here are errors and warnings that I got from the compiler:

../src/temp.c: In function 'main':
../src/temp.c:19: warning: passing argument 1 of 'allocate_struct_array' from incompatible pointer type
../src/temp.c: In function 'allocate_struct_array':
../src/temp.c:32: error: incompatible types in assignment
../src/temp.c:42: error: incompatible types in assignment
../src/temp.c:44: error: wrong type argument to unary exclamation mark
../src/temp.c:50: error: invalid type argument of '->'
make: *** [src/temp.o] Error 1
  • 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-05-27T10:13:45+00:00Added an answer on May 27, 2026 at 10:13 am

    I think you missed the correctness of the original answer, so I will elaborate here…

    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct record
    {
        char * community_name;
        double data[10];
        double crimes_per_pop;
    };
    
    void allocate_struct_array(struct record ** array_pointer, int length);
    /* the allocate_struct_array should take a POINTER to a POINTER */
    
    int main()
    {
        int num_lines = 10;
    /* this is wrong:   struct record ** data_array; */
    /* it should be: */ struct record *data_array; 
    
        allocate_struct_array(&data_array, num_lines); /* NOW this will work */
    
        data_array[0]->community_name[0] = 'h'; /* THIS WILL STILL CRASH community_name is not allocated!!! */
        printf("%c\n", data_array[0]->community_name[0]);
    
        return 0;
    }
    
    
    void allocate_struct_array(struct record **array_pointer, int length)
    {
        int i;
        struct record *array;
        array = malloc(length * sizeof(struct record *));
    
        if (!array)
        {
            fprintf(stderr, "Could not allocate the array of struct record *\n");
            exit(1);
        }
    
        for (i = 0; i < length; i++)
        {
            array[i] = malloc( sizeof(struct record) );
    
            if (!array[i])
            {
                fprintf(stderr, "Could not allocate array[%d]\n", i);
                exit(1);
            }
        }
        *array_pointer = array; /* copied over. */
    }
    

    By the way i would like to show you a good C implementation of structure allocations record:

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

Sidebar

Related Questions

I'm trying to create an array of a struct, which I have done and
I have dynamic array in struct and a method that uses the dynamic array.
I need to create an array of arrays. I have been using array_map(null,$a,$b,$c) to
I have a small assignment in C. I am trying to create an array
I need to create a big array in my code, I have the values
Hi I want to create a JSON array. I have tried using: JSONArray jArray
I have a 3x3 array that I'm trying to create a pointer to and
Consider I have an array of elements out of which I want to create
Suppose I have an array of nodes (objects). I need to create a duplicate
I am trying to create a re-sizable array of CGPoints in objective-c. I've have

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.