From bf5ad270649c8abea0223d13fda99736be62977e Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Thu, 24 Apr 2025 23:08:32 +0200 Subject: [PATCH] better example - more real usecase --- array_handle_test.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 array_handle_test.c diff --git a/array_handle_test.c b/array_handle_test.c new file mode 100644 index 0000000..ba470ab --- /dev/null +++ b/array_handle_test.c @@ -0,0 +1,32 @@ +#include +#include +#include "handle.h" + +struct Vektor { + int count; + int *v; +}; handle(Vektor) { + if(state == HANDLE_CREAT) { + this->count = *(int*) data; + this->v = calloc(this->count, sizeof(int)); + } else if (state == HANDLE_DESTR){ + if(this->v) free(this->v); + } +} + +int main() { + int n; + printf("Number of elements to sum: "); + scanf(" %d", &n); + creat(Vektor, data, &n); + // printf("vektor.count:%d\n", data.count); + + for(int i = 0; i < data.count; ++i) { + scanf(" %d", &n); + data.v[i] = n; + } + n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i]; + printf("Sum: %d\n", n); + + return 0; +}