summaryrefslogtreecommitdiff
path: root/main_dyn.c
diff options
context:
space:
mode:
authorClément Zrounba <6691770+clement-z@users.noreply.github.com>2020-12-21 23:48:40 +0100
committerClément Zrounba <6691770+clement-z@users.noreply.github.com>2020-12-21 23:48:40 +0100
commit316713591f69756cf786954c5cb7a38e73ec78eb (patch)
treec4d44fcd6287e29493bb0524f0b635081be9d68e /main_dyn.c
downloadarray_c_lulu-master.tar.gz
array_c_lulu-master.zip
initial commitHEADmaster
Diffstat (limited to 'main_dyn.c')
-rw-r--r--main_dyn.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/main_dyn.c b/main_dyn.c
new file mode 100644
index 0000000..267b7f5
--- /dev/null
+++ b/main_dyn.c
@@ -0,0 +1,50 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+struct va {
+ int n;
+ int nmax;
+ int *data;
+};
+
+void create_array(struct va *va, int nmax) {
+ va->n = 0;
+ va->nmax = nmax;
+ va->data = (int *) malloc(nmax * sizeof(int));
+ if(va->data == NULL)
+ // no space left to allocate data...
+ return;
+}
+
+void init_array(struct va *va, int n, int *src) {
+ if(n > va->nmax)
+ return;
+ va->n = n;
+ memcpy(va->data, src, n*sizeof(int));
+}
+
+void free_array(struct va *va) {
+ if(va->data)
+ free(va->data);
+ va->nmax = 0;
+ va->n = 0;
+ va->data = NULL;
+}
+
+int main(int argc, char *argv[]) {
+ struct va va;
+ int t[5] = {1,2,3,4,5};
+ int i;
+
+ create_array(&va, 12);
+ init_array(&va, 5, t);
+
+ for(i = 0; i < va.n; ++i) {
+ printf("%d\n", va.data[i]);
+ }
+
+ free_array(&va);
+
+ return 0;
+}