└── README.md /README.md: -------------------------------------------------------------------------------- 1 | // C program to create dynamic array using malloc() function 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | // address of the block created hold by this pointer 10 | int* ptr; 11 | int size; 12 | 13 | // Size of the array 14 | printf("Enter size of elements:"); 15 | scanf("%d", &size); 16 | 17 | // Memory allocates dynamically using malloc() 18 | ptr = (int*)malloc(size * sizeof(int)); 19 | 20 | // Checking for memory allocation 21 | if (ptr == NULL) { 22 | printf("Memory not allocated.\n"); 23 | } 24 | else { 25 | 26 | // Memory allocated 27 | printf("Memory successfully allocated using " 28 | "malloc.\n"); 29 | 30 | // Get the elements of the array 31 | for (int j = 0; j < size; ++j) { 32 | ptr[j] = j + 1; 33 | } 34 | 35 | // Print the elements of the array 36 | printf("The elements of the array are: "); 37 | for (int k = 0; k < size; ++k) { 38 | printf("%d, ", ptr[k]); 39 | } 40 | } 41 | 42 | return 0; 43 | } 44 | --------------------------------------------------------------------------------