From 8b1303304d7170142c2e4172fc4e65812c28d61f Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Fri, 6 Oct 2023 16:59:12 +0100 Subject: [PATCH] c16 --- C16/ex2.c | 18 ++++++++++++++++++ C16/ex3.c | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 C16/ex2.c create mode 100644 C16/ex3.c diff --git a/C16/ex2.c b/C16/ex2.c new file mode 100644 index 0000000..6137a98 --- /dev/null +++ b/C16/ex2.c @@ -0,0 +1,18 @@ +#include +#include + +int main(void) +{ + // Declare an integer array and an integer pointer + int *pData; + int datlen; + + printf("Length of data to be allocated: "); + scanf("%d", &datlen); + + // Using malloc + pData = malloc ( datlen * sizeof (int)); + + free(pData); + return 0; // Exit +} diff --git a/C16/ex3.c b/C16/ex3.c new file mode 100644 index 0000000..9a2323c --- /dev/null +++ b/C16/ex3.c @@ -0,0 +1,23 @@ +#include +#include + +int main(void) +{ + // Declare an integer array and an integer pointer + int *pData; + int datlen; + + printf("Length of data to be allocated: "); + scanf("%d", &datlen); + + // Using malloc + pData = malloc ( datlen * sizeof (int)); + + for (int i = 0; i < datlen; i++) { + pData[i] = i; + printf("%d %d\n", i, i); + } + + free(pData); + return 0; // Exit +}