Allocating works the same for all types. If you need to allocate an array of line structs, you do that with:

struct line* array = malloc(number_of_elements * sizeof(struct line));

In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc().

Note that's it's better style to use:

sizeof(*array)

instead of:

sizeof(struct line)

The reason for this is that the allocation will still work as intended in case you change the type of array. In this case this is unlikely, but it's just a general thing worth getting used to.

Also note that it's possible to avoid having to repeat the word struct over and over again, by typedefing the struct:

typedef struct line
{
    char* addr;
    char* inst;
} line;

You can then just do:

line* array = malloc(number_of_elements * sizeof(*array));

Of course don't forget to also allocate memory for array.addr and array.inst.

Answer from Nikos C. on Stack Overflow
Top answer
1 of 2
37

Allocating works the same for all types. If you need to allocate an array of line structs, you do that with:

struct line* array = malloc(number_of_elements * sizeof(struct line));

In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc().

Note that's it's better style to use:

sizeof(*array)

instead of:

sizeof(struct line)

The reason for this is that the allocation will still work as intended in case you change the type of array. In this case this is unlikely, but it's just a general thing worth getting used to.

Also note that it's possible to avoid having to repeat the word struct over and over again, by typedefing the struct:

typedef struct line
{
    char* addr;
    char* inst;
} line;

You can then just do:

line* array = malloc(number_of_elements * sizeof(*array));

Of course don't forget to also allocate memory for array.addr and array.inst.

2 of 2
10

For what you have described, You do not need to allocate memory for your struct, rather, you need to allocate memory for the members char *addr;, and char *inst;. If you want to have a single copy of that structure, the first section of code illustrates how to initialize, and assign values. If you want an array, the second code example illustrates the differences.

This illustrates how to allocate memory for the members of a single struct line:

typedef struct
{
    char* addr;
    char* inst;
}LINE;

LINE line;  

int main(void)
{   

    strcpy(line.addr, "anystring"); //will fail
    line.addr = malloc(80);
    line.inst = malloc(80);
    strcpy(line.addr, "someString");//success;
    strcpy(line.inst, "someOtherString");//success;

}

For array of struct line...

typedef struct
{
    char* addr;
    char* inst;
}LINE;  //same struct definition

LINE line[10]; //but create an array of line here.

int main(void)
{   
    int i;
    
    for(i=0;i<10;i++)
    {
      line[i].addr = malloc(80);
      line[i].inst = malloc(80);
    }

    for(i=0;i<10;i++)
    {
        strcpy(line[i].addr, "someString");
        strcpy(line[i].inst, "someOtherString");
    }
    //when done, free memory
    for(i=0;i<10;i++)
    {
        free(line[i].addr);
        free(line[i].inst);
    }      


}

Added to address comment
Addressing the comment under this answer from @Adam Liss, the following code illustrates the following improvements using strdup(): 1) Uses only memory needed. 2) Performs memory creation and copy operations in one step, so the the following blocks:

for(i=0;i<10;i++)
{
  line[i].addr = malloc(80);
  line[i].inst = malloc(80);
}

for(i=0;i<10;i++)
{
    strcpy(line[i].addr, "someString");
    strcpy(line[i].inst, "someOtherString");
}

Become:

for(i=0;i<10;i++)
{
  line[i].addr = strdup("someString");
  line[i].inst = strdup("someOtherString");
}

One more note: Error handling was not included in examples above to avoid muddling up focus on the main concepts: But for the sake of completeness, because both malloc() and strdup() can fail, actual usage for each of these two functions, should include a test before using, eg:

Rather than

  line[i].addr = strdup("someString");
  line[i].inst = strdup("someOtherString");

The code should include

  line[i].addr = strdup("someString");
  if(!line[i].addr)
  {
      //error handling code here
  }
  line[i].inst = strdup("someOtherString");
  if(!line[i].inst)
  {
      //error handling code here
  }
๐ŸŒ
Quora
quora.com โ€บ How-do-I-malloc-an-array-of-structs-in-C
How to malloc() an array of structs in C - Quora
Then, [code]struct X *pArray = malloc(sizeof(struct X) * N); if (pArray) { // Access elements using pArray } [/code]Itโ€™s really that simple. You can use pArray with square bracket ...
Discussions

c - malloc an array of struct pointers - Stack Overflow
I have the following struct: typedef struct _chess { int **array; int size; struct _chess *parent; } chess; and I have: typedef struct _chess *Chess; Now, I want to create an array of dy... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 9, 2015
How to malloc for an array of structs in c - Stack Overflow
I have searched for a way to do this on my own, but I haven't found a situation that exactly matches mine and I'm not experienced enough to derive what to do from similar situations. So I'm hoping... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 13, 2014
c - Malloc an array inside a struct - Stack Overflow
I'm trying to malloc an array inside a struct but I keep getting segmentation errors when I run the program. The compares function is just something I'm testing so it shouldn't be a part of the pr... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 19, 2015
c - Malloc an array of structs. - Stack Overflow
Have I gone about using malloc the correct way with the temporary structure? I am getting the error 'dereferencing to incomplete type' How would I then go about creating an array of valid and invalid structures that are malloced? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to malloc arrays inside structs?
r/C_Programming on Reddit: How to malloc arrays inside structs?
June 15, 2022 -

I would like to have a struct containing several dynamically allocated arrays. I've tried several variations of the following code:

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

struct rectangle 
{
    int 
    width,
    height;
    int* 
    arr1,
    arr2;
};

int main()
{       
    int n = 3;
    struct rectangle box1 = 
    {
       .width = 3,
       .height = 4,
       .arr1 = (int*) malloc( n * sizeof(int)),
       .arr2 = (int*) malloc( n * sizeof(int))
    }; 
}

However, I keep getting warning: initialization from int to int* makes integer from pointer without a cast [-Wint-conversion].

๐ŸŒ
Diveintosystems
diveintosystems.org โ€บ book โ€บ C2-C_depth โ€บ structs.html
2.7. C Structs
Figure 1 sketches what the variables s and sptr may look like in memory after the code above executes. Recall that malloc allocates memory from the heap, and local variables are allocated on the stack. Figure 1. The differences in memory layout between a statically allocated struct (data on ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-dynamically-create-array-of-structs-in-c
How to Dynamically Create Array of Structs in C? - GeeksforGeeks
July 23, 2025 - The malloc function returns the pointer of void type to the memory allocated of the given size. We then use typecasting to cast this pointer to the desired type which here is the pointer to struct.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ array-of-structs-malloc
How to Use Malloc Function to Create Array of Structs โ€“ Linux Hint
If you donโ€™t want to put a limit on the number of characters for the employee name then you can simply take the input first for the employee name inside the for loop within a new variable and then pass that variable size in the malloc function. The struct data type in C programming provides better performance when we have to deal with small groups of the same values. In this write-up, we have discussed the creation of structs with arrays using the dynamic memory function that is malloc() function.
๐ŸŒ
Nyu-cso
nyu-cso.github.io โ€บ notes โ€บ 06-struct-malloc.pdf pdf
Struct, malloc Jinyang Li
//do something with the array ยท โ€ฆ ยท } int *newArray(int n) { int *p; p = (int*)malloc(sizeof(int)*n); return p; } Conceptual view of a C programโ€™s ยท memory at runtime ยท โ€ข Separate memory regions for global, local, and ยท malloc-ed. ..... ... Static data ยท (global variables) Heap ยท (for malloced data) Stack ยท (for local variables) We will refine this simple view ยท in later lectures ยท Linked list ยท typedef struct { int val; struct node *next; }node; val:5 ยท
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ c malloc struct
How to Allocate Struct Memory With malloc in C | Delft Stack
February 12, 2024 - In this method, memory is allocated to store an array of structures using malloc. The following example code demonstrates the case when the array of 100 pointers to the MyObject structs is declared on the stack, but each individual MyObject object is allocated on dynamic memory (heap).
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ c array of structs
How to Create Array of Structs in C | Delft Stack
February 12, 2024 - Its syntax is as follows: ... Here, ... the case of creating an array of structs, we determine the size by multiplying the number of elements by the size of each struct using sizeof....
๐ŸŒ
Swarthmore College
cs.swarthmore.edu โ€บ ~newhall โ€บ cs31 โ€บ resources โ€บ C-structs_pointers.php
CS31: Intro to C Structs and Pointers
malloc is often used to allocate an array of some type on the heap, by specifying the total number of bytes in the array using an expression of (size of the type of each bucket and the number of buckets: int *arr; char *c_arr; // allocate an array of 20 ints on the heap: arr = (int ...
Top answer
1 of 4
49

array is a slightly misleading name. For a dynamically allocated array of pointers, malloc will return a pointer to a block of memory. You need to use Chess* and not Chess[] to hold the pointer to your array.

Chess *array = malloc(size * sizeof(Chess));
array[i] = NULL;

and perhaps later:

/* create new struct chess */
array[i] = malloc(sizeof(struct chess));

/* set up its members */
array[i]->size = 0;
/* etc. */
2 of 4
23

There's a lot of typedef going on here. Personally I'm against "hiding the asterisk", i.e. typedef:ing pointer types into something that doesn't look like a pointer. In C, pointers are quite important and really affect the code, there's a lot of difference between foo and foo *.

Many of the answers are also confused about this, I think.

Your allocation of an array of Chess values, which are pointers to values of type chess (again, a very confusing nomenclature that I really can't recommend) should be like this:

Chess *array = malloc(n * sizeof *array);

Then, you need to initialize the actual instances, by looping:

for(i = 0; i < n; ++i)
  array[i] = NULL;

This assumes you don't want to allocate any memory for the instances, you just want an array of pointers with all pointers initially pointing at nothing.

If you wanted to allocate space, the simplest form would be:

for(i = 0; i < n; ++i)
  array[i] = malloc(sizeof *array[i]);

See how the sizeof usage is 100% consistent, and never starts to mention explicit types. Use the type information inherent in your variables, and let the compiler worry about which type is which. Don't repeat yourself.

Of course, the above does a needlessly large amount of calls to malloc(); depending on usage patterns it might be possible to do all of the above with just one call to malloc(), after computing the total size needed. Then you'd still need to go through and initialize the array[i] pointers to point into the large block, of course.

๐ŸŒ
Dartmouth College
cs.dartmouth.edu โ€บ ~campbell โ€บ cs50 โ€บ dynamicmem.html
Pointers and Dynamic Memory Allocation
The memory management functions use โ€œpointer to voidโ€ (void *) which allows the programmer to assign the returned pointer to memory to any struct, array, variable without casting. This is a very good example of the use of pointer to void. Consider the following example which allocates space for a new copy of a given string. This is very similar to the standard function named strdup(): // malloc returns a pointer to void for the number of bytes requested void *malloc(unsigned int nbytes); char *newstr(const char *s) { char *p; if( (p=malloc(strlen(s)+1)) == NULL ) { fprintf(stderr,"out of memory!\n"); exit(1); } strcpy(p,s); return(p); }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-create-dynamic-array-of-structs-in-c
How to Create a Dynamic Array of Structs? - GeeksforGeeks
July 23, 2025 - We can create a dynamic array of structs using the malloc() funciton. This function takes the required size of the memory as an argument and returns the void pointer to the allocated memory.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 131225-malloc-array-structures.html
malloc() with array of structures
October 25, 2010 - "Finding the smallest program that demonstrates the error" is a powerful debugging tool. Look up a C++ Reference and learn How To Ask Questions The Smart Way ... typedef struct { int roll; }REC; int main() { int i, n =3; REC *pt= malloc(n*sizeof(*pt)); printf("\n* * * * Enter roll no of students.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-make-an-array-of-pointers-to-structs-in-c
How to make an array of pointers to structs in c - Quora
Answer (1 of 9): [code]typedef struct { //stuff that goes in struct here } foo; int main() { foo localArray[100]; //example of stack-allocated //struct array foo* dynArray = (foo*)malloc(100*sizeof(foo)); //example of //heap allocated //struct array fo...
Top answer
1 of 3
3

Your problem is the way you declared the struct it should be

Copystruct data 
{
    int *ref; 
    int *port;
    char data[MAX_STRING];
};

and then you do

Copystruct data *p_valid;
p_valid = malloc(sizeof(struct data));

another thing is

Copyp_valid->data = malloc(sizeof(STRINGMAX));

is wrong because data is not a pointer. And sizeof(STRINGMAX) is wrong too, since STRINGMAX seems to be a macro and hence it will expand to it's value say if you have #define STRINGMAX 4 then it would expand to sizeof(4).

2 of 3
1
CopyYour definition of the struct:

struct data {
    int *ref; 
    int *port;
    char data[MAX_STRING];
}temp, valid, invalid;

should be more like this:

struct data 
{
    int *ref; 
    int *port;
    char data[MAX_STRING];
};

then define the arrays similar to this:

struct data temp;

struct data* valid = NULL;
struct data* invalid = NULL;
int currentValidSize = 0;
int currentInvalidSize = 0;
struct data * validTemp = NULL;
struct data * invalidTemp = NULL;

then, each time the code needs room for (another) instance of a struct

struct data *validTemp = realloc(valid, (currentValidSize+1)* sizeof(data) );
if( NULL == validTemp )
{ // realloc failed
    perrof( "realloc failed" );

    // free everything, close files, etc here probably be writing a sub function
    // and calling it here.
    // a sub function that: 
    // that walks the valid and invalid arrays, 
    // first free'ing any malloc'd fields
    // then finally free'ing the whole array

    exit( EXIT_FAILURE );
}

// implied else, realloc successful

// update array size counter
currentValidSize++;

// update ptr to valid array of structs
valid = validTemp;
validTemp = NULL;

similar for adding an entry to the invalid array of structs

then update the valid array of structs from temp as:
(note the '-1' in the offset into valid[])

memcpy( &valid[currentValidSize-1], &temp, sizeof data );
// Note you will also have to perform a 'deep' copy of any areas 
// that were malloc'd within the 'temp' struct
Top answer
1 of 6
2

If I had your code and had to improve it, I'd go for

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

// The kernel style guide https://www.kernel.org/doc/html/v4.10/process/coding-style.html discourages typedefs for structs
typedef struct moeda {
    double *return_value;
} moeda;


// return a struct here:
moeda initialize_return(int a)
{
    moeda ret;
    ret.return_value = malloc(a*sizeof(double));
    return ret;
}


int main(void) {
    long int a=250;

    moeda m = initialize_return(a);

    m.return_value[0] = 2000;
    printf("%lf", m.return_value[0]);

    return 0;
}

(it is better to have all identifiers in English).

This would be the first step to do. Then I might realize that the struct isn't really needed and replace it:

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

double * initialize_double_array(int a)
{
    return malloc(a*sizeof(double));
}

int main(void) {
    long int a=250;

    double * arr = initialize_double_array(a);

    arr[0] = 2000;
    printf("%lf", arr[0]);

    return 0;
}

OTOH, if there are other fields in the said struct, I might decide if they are supposed to be initialized along with this array or not.

Some variants:

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

// The kernel style guide https://www.kernel.org/doc/html/v4.10/process/coding-style.html discourages typedefs for structs
struct moeda {
    int num_values;
    double *values;
};

// only fill a struct here:
// i. e. take a pre-initialized struct and work with it:
void moeda_alloc_values(struct moeda * data) 
{
    data->return_value = malloc(data->num_values * sizeof(double));
}

// return a struct here:
struct moeda initialize_moeda(int num) 
{
    struct moeda ret;
    ret.num_values = num;
    ret.return_value = malloc(num * sizeof(double));
    // or just moeda_alloc_values(&ret);
    return ret;
}

int main(void) {
    long int a=250;

    struct moeda m = initialize_return(a);
    m.return_value[0] = 2000;
    printf("%lf", m.return_value[0]);

    struct moeda m2;
    m2.num_values = 20;
    moeda_alloc_values(&m2);
    m2.return_value[0] = 2000;
    printf("%lf", m2.return_value[0]);

    return 0;
}

The struct returning function has the advantage that you have a "readily filled" structure after return.

The other function which modifies a struct via a pointer on it has the advantage that it can work on any, possibly pre-filled, possibly malloced struct and that it can work on single fields instead having to consider all fields.

2 of 6
2

First of all return is a reserved keyword in C and you can not use a reserved keyword as a variable name.

Second,If you want to allocate memory for array of any data type in other function then,declare a variable in the function,call malloc,allocate required space through malloc and return the address of the first element of allocated space.If you do not return the address the allocated space would not be known to the called function(here main()) and it could not be able to access the allocated memory space.You can do like this:

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

typedef struct
{
   double *var;
}moeda;


double *ini(int n)
{
   double *arr;
   arr = malloc(n*sizeof(*arr));
   return arr;
}

int main(void){

   long int a=250;

    moeda m;

    m.var=ini(a);

    m.var[0] = 2000;
    printf("%lf", m.var[0]);



    return 0;
}
Top answer
1 of 5
10

A struct included inside another struct is contained by copy, so you would not have to separately malloc it. If the struct contains a pointer to another struct, then you can consider allocating memory for it dynamically.

struct Point2d
{
    float x;
    float y;
};

struct Rect
{
    struct Point2D a;
    struct Point2D b;
};

struct LinkedListNode
{
    struct LinkedListNode* next;
    int value;
};

In struct Rect, the struct Point2D element are inserted into struct Rect and you don't have to dynamically allocate memory for them. On the contrary in the struct LinkedListNode the next element is referenced by a pointer and the memory must be dynamically allocated.

The two version are both useful, depending on the situation. There is no correct way to manage memory, it'll depend on your usage.

This same situation occurs in the case of an array. If your array is statically sized, then it can be directly included in the struct. However, if the size can vary, you must store a pointer within the struct.

struct Header
{
    char magic[4];
    unsigned int width;
    unsigned int height;
};

struct Buffer
{
    char* data;
    unsigned int size;
    unsigned int capacity;
};

struct Buffer* buffer_init()
{
    struct Buffer* buffer = (struct Buffer*)malloc(sizeof(struct Buffer));
    buffer->data = 0;
    buffer->size = 0;
    buffer->capacity = 0;
}

void buffer_grow(struct Buffer* buffer, size_t capacity)
{
    if (capacity > buffer->capacity)
    {
        buffer->data = realloc(buffer->data, capacity);
        buffer->capacity = capacity;
    }
}

void buffer_append(struct Buffer* buffer, const char* data, unsigned int dataLen)
{
    if (dataLen + buffer->size > buffer->capacity)
        buffer_grow(MAX(dataLen + buffer->size, buffer->capacity * 2));

    memcpy(buffer->data + buffer->size, data, dataLen);
    buffer->size += dataLen;
}

The realloc function only does a shallow copy, that is pointer value is copied, but not the pointed object. One more time, how you deal with it will depend on your application.

2 of 5
2
typedef struct _A
{
  int *arr;
  int arrCount;
} A;

void Construct_A(A *a, int arraySize)
{
  a->arrCount = arraySize;
  a->arr = (int*)malloc(sizeof(int)*arraySize);
}

void Destruct_A(A *a)
{
  free(a->arr);
  a->arr = 0;
}

typedef struct _B
{
  A *a;
} B;

void Construct_B(B *b, int arraySize_A)
{
  b->a = (A*)malloc(sizeof(A));
  Construct_A(b->a);
}

void Destruct_B(B *b)
{
  Destruct_A(b->a);
  free(b->a);
  b->a = 0;
}

void main()
{
  B b;
  Construct_B(&b, 10);

  // Use b and b->a

  Destruct_B(&b);
}