Note that there are some explanatory texts on larger screens.

plurals
  1. POTask to create flexible (dynamic memory allocation) array
    primarykey
    data
    text
    <p>We've been given a task to write a dynamic data structure in C. I'm still incredibly new to C and I would really appreciate it if someone could give me some pointers (lol) on what I'm doing wrong / what I should read more about.</p> <p>Here's what I have so far:</p> <p><strong>flexarray.h</strong></p> <pre><code>#ifndef FLEXARRAY_H #define FLEXARRAY_H typedef struct flexarrayrec flexarray; flexarray *flexarray_new(); extern void flexarray_add(flexarray *a, char item); extern void flexarray_set(flexarray *a, char item, int index); extern char flexarray_get(flexarray *a, int index); extern void flexarray_trim(flexarray *a); extern void flexarray_print(flexarray *a); #endif /* FLEXARRAY_H */ </code></pre> <p><strong>flexarray.c</strong></p> <pre><code>#include "flexarray.h" #include "mylib.h" #define DEFAULT_CAPACITY 10 typedef struct flexarray{ int size; int capacity; char *array; }; flexarray *flexarray_new(){ flexarray a; a-&gt;capacity = DEFAULT_CAPACITY; a-&gt;size = 0; a-&gt;array = (char*) erealloc (a ,a-&gt;capacity * sizeof(char)); return a; } void grow(flexarray a){ a-&gt;capacity = a-&gt;capacity * 2; a-&gt;array = (int*) erealloc (a ,a-&gt;capacity * sizeof(int)); } void flexarray_add(flexarray a, char item){ if(a-&gt;size == a-&gt;capacity) grow(a); a-&gt;array[a-&gt;size - 1] = item; a-&gt;size++; } void set(flexarray a, char c, int index){ if(index &lt; 0 || index &gt; a-&gt;size) return; a-&gt;array[index] = c; } char get(flexarray a, int index){ if(index &lt; 0 || index &gt; a-&gt;size) return; return a-&gt;array[index]; } void flexarray_trim(flexarray a){ if(a-&gt;size == a-&gt;capacity) return; a-&gt;capacity = a-&gt;size; a-&gt;array = (char*) erealloc (a, a-&gt;capacity * sizeof(char)); } void flexarray_print(flexarray a){ int i; printf("["); for(i = 0; i &lt; a-&gt;size - 1; i++){ printf("%c,", a-&gt;array[i]); } printf("%c]\n", a-&gt;array[i + 1]); } </code></pre> <p>Thank you</p> <p><strong>EDIT</strong> Current compile log:</p> <pre><code>flexarray.c:10:1: warning: useless storage class specifier in empty declaration [enabled by default] flexarray.c: In function 'flexarray_new': flexarray.c:13:15: error: storage size of 'a' isn't known flexarray.c: At top level: flexarray.c:20:21: error: parameter 1 ('a') has incomplete type flexarray.c:25:30: error: parameter 1 ('a') has incomplete type flexarray.c:31:20: error: parameter 1 ('a') has incomplete type make[2]: *** [build/Debug/MinGW_Actual-Windows/flexarray.o] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2 BUILD FAILED (exit value 2, total time: 1s) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload