diff --git a/array_handle_gen_test.c b/array_handle_gen_test.c index fed0659..85d7f84 100644 --- a/array_handle_gen_test.c +++ b/array_handle_gen_test.c @@ -10,9 +10,9 @@ int main() { size_t n = 0; printf("Number of elements to sum: "); scanf(" %zu", &n); - /* Create a 64 bit array of 'n' elements */ + + /* Create a 64 bit array of 'n' elements named 'data' */ creat(Array_uint64, data, &n); - // printf("vektor.count:%d\n", data.count); for(int i = 0; i < data.count; ++i) { scanf(" %zu", &n); @@ -22,5 +22,6 @@ int main() { printf("Sum: %zu\n", n); // RAII releases array here automagicaly! + return 0; } diff --git a/array_handle_test.c b/array_handle_test.c index 6aaaea1..78717c7 100644 --- a/array_handle_test.c +++ b/array_handle_test.c @@ -6,10 +6,10 @@ struct Vektor { int count; int *v; }; handle(Vektor) { - if(state == HANDLE_CREAT) { + if(HANDLE_CREAT == state) { self->count = *(int*) data; self->v = (int*) calloc(self->count, sizeof(int)); - } else if (state == HANDLE_DESTR){ + } else if (HANDLE_DESTR == state){ if(self->v) free(self->v); } } @@ -19,7 +19,7 @@ int main() { printf("Number of elements to sum: "); scanf(" %d", &n); creat(Vektor, data, &n); - // printf("vektor.count:%d\n", data.count); + // printf("data.count:%d\n", data.count); for(int i = 0; i < data.count; ++i) { scanf(" %d", &n); diff --git a/array_test.c b/array_test.c index f7ca375..62c3093 100644 --- a/array_test.c +++ b/array_test.c @@ -14,6 +14,14 @@ Vektor create_Vektor(int count) { return self; } +int sum_Vektor(Vektor *self) { + int s = 0; + for(int i = 0; i < self->count; ++i) { + s += self->v[i]; + } + return s; +} + void delete_Vektor(Vektor *self) { if(self->v) free(self->v); } @@ -29,7 +37,8 @@ int main() { scanf(" %d", &n); data.v[i] = n; } - n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i]; + // n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i]; + n = sum_Vektor(&data); printf("Sum: %d\n", n); // Need to not forget this diff --git a/construct_destruct.c b/construct_destruct.c index 47b9745..07a4baa 100644 --- a/construct_destruct.c +++ b/construct_destruct.c @@ -6,8 +6,8 @@ void __attribute__((destructor)) calledLast(); int main() { printf("\nI am in main"); - // return 0; - exit(0); // Even called on this!!! + return 0; + // exit(0); // Even called on this!!! } void calledFirst() { diff --git a/handle.h b/handle.h index 9b0b0f0..215e6b9 100644 --- a/handle.h +++ b/handle.h @@ -1,6 +1,7 @@ #ifndef MAG_HANDLE_H #define MAG_HANDLE_H /* Simple single-header library that makes you have RAII in gcc (also works in C++ compilers) */ +/* Licence: CC-BY */ /** Tells your constructor/destructor handler function about the state */ enum HANDLE_STATE { diff --git a/handle_test.c b/handle_test.c index c7a0e2a..57feca3 100644 --- a/handle_test.c +++ b/handle_test.c @@ -8,6 +8,7 @@ struct Meaning { handle(Meaning) { if(state == HANDLE_CREAT) { + // Constructor self->a = *(int*) data; self->b = 2; } else { diff --git a/innerf.c b/innerf.c index 4938601..96e7021 100644 --- a/innerf.c +++ b/innerf.c @@ -1,18 +1,27 @@ #include int main() { - int a = 0; - int b = 40; - int c = 2; + auto int a = 0; + int b = 20; + int c = 1; + int d = 0; // See: https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Nested-Functions.html - inline void lf(register int* r) { + inline auto void lf(register int *r) { a += *r; + int s = a + *r; + inline void deepfun() { + s *= 2; + } + + deepfun(); + d += s; }; // semicolon(;) not needeed! /* // Same as above... but need auto storage class if you pre-declare inline auto void lf(register int* r); + // ... inline void lf(register int* r) { a += *r; }; // semicolon(;) not needeed! @@ -21,6 +30,6 @@ int main() { lf(&b); lf(&c); - printf("%d\n", a); + printf("%d .. %d\n", a, d); }