From 006e787526958c873246aa063dd8600480fdf8ba Mon Sep 17 00:00:00 2001 From: Kushal Date: Wed, 23 Oct 2019 22:23:11 +0530 Subject: [PATCH] included dynamic array in c --- dynamic_array.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 dynamic_array.c diff --git a/dynamic_array.c b/dynamic_array.c new file mode 100644 index 00000000..5a9494c4 --- /dev/null +++ b/dynamic_array.c @@ -0,0 +1,27 @@ +#include +#include +int main() +{ + int n, i, *ptr, sum = 0; + printf("Enter number of elements: "); + scanf("%d", &n); + ptr = (int*) malloc(n * sizeof(int)); + + // if memory cannot be allocated + if(ptr == NULL) + { + printf("Error! memory not allocated."); + exit(0); + } + printf("Enter elements: "); + for(i = 0; i < n; ++i) + { + scanf("%d", ptr + i); + sum += *(ptr + i); + } + printf("Sum = %d", sum); + + // deallocating the memory + free(ptr); + return 0; +} \ No newline at end of file