How to Create a Dynamic Array Inside a Structure? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In C, the structure can store the array data types as one of its members. In this article, we will learn how to create a dynamic array inside a structure in C. Creating a Dynamic Array Inside a Structure in CTo create a dynamic array inside a structure in C, define a structure that contains a pointer to the array type and a variable to store the size of the array. Then initialize the dynamic array with that size using malloc() to dynamically allocate memory for the array. Syntax to Create a Dynamic Array Inside a Structure// Define the structurestruct StructName { dataType* arrayName; int size;};// In function StructName variableName;variableName.arrayName = new dataType[size];variableName.size = size;Here, StructName is the name of the structure.dataType is the type of data that the array holdsarrayName is the name of the array.variableName is the name of the structure variable.size is the size of the array.C++ Program to Create a Dynamic Array Inside a StructureThe below program demonstrates how we can create a dynamic array inside a structure in C++. C // C Program to illustrate how to create a dynamic array // inside a structure #include <stdio.h> #include <stdlib.h> // Define the structure typedef struct myStruct { int* myArray; int size; } MyStruct; int main() { // Define the size of the array int size = 5; // Create a structure variable MyStruct s; // Create a dynamic array inside the structure s.myArray = (int*)malloc(sizeof(int) * size); s.size = size; // Initialize the array for (int i = 0; i < size; i++) { s.myArray[i] = i; } // Print the array elements for (int i = 0; i < size; i++) { printf("%d ", s.myArray[i]); } // Delete the dynamic array free(s.myArray); return 0; } Output0 1 2 3 4 Time Complexity: O(N), here N is the size of the array.Auxiliary Space: O(N) Comment A anjalibo6rb0 Follow 0 Improve A anjalibo6rb0 Follow 0 Improve Article Tags : C Programs C Language c-array C-Arrays C-Structure & Union +1 More Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C6 min readPointers and StructuresPointers in C9 min readFunction Pointer in C6 min readUnions in C4 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like