0

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.

Here's what I have so far:

flexarray.h

#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 */

flexarray.c

#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->capacity = DEFAULT_CAPACITY;
    a->size = 0;
    a->array = (char*) erealloc (a ,a->capacity * sizeof(char)); 
    return a;
}

void grow(flexarray a){
    a->capacity = a->capacity * 2;
    a->array = (int*) erealloc (a ,a->capacity * sizeof(int)); 
}

void flexarray_add(flexarray a, char item){
    if(a->size == a->capacity) grow(a);
    a->array[a->size - 1] = item;
    a->size++;    
}

void set(flexarray a, char c, int index){
    if(index < 0 || index  > a->size) return;
    a->array[index] = c;
}

char get(flexarray a, int index){
    if(index < 0 || index  > a->size) return;
    return a->array[index];
}


void flexarray_trim(flexarray a){
    if(a->size == a->capacity) return;
    a->capacity = a->size;
    a->array = (char*) erealloc (a, a->capacity * sizeof(char)); 
}

void flexarray_print(flexarray a){
    int i;
    printf("[");
    for(i = 0; i < a->size - 1; i++){
        printf("%c,", a->array[i]);
    }
    printf("%c]\n", a->array[i + 1]);
}

Thank you

EDIT Current compile log:

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)
4
  • 3
    What is the question? What doesn't work? What doesn't compile? Commented May 3, 2012 at 15:48
  • You probably have to write a small main() function that will test your data structure. With that main you can test compilation and debug... Commented May 3, 2012 at 15:52
  • @eyalm I've excluded the main function, as it's not relevant to the question. Commented May 3, 2012 at 15:57
  • @ssteinberg I'm getting a variety of compiling errors (which I'll add to the question now). Commented May 3, 2012 at 16:00

3 Answers 3

4
void flexarray_add(flexarray a, char item){
    if(a->size == a->capacity) grow(a);
    a->array[a->size - 1] = item;
    a->size++;    
}

Should be:

void flexarray_add(flexarray *a, char item){
    if(a->size == a->capacity) grow(a);
    a->array[a->size] = item;
    a->size++;    
}

And:

void flexarray_print(flexarray a){
    int i;
    printf("[");
    for(i = 0; i < a->size - 1; i++){
        printf("%c,", a->array[i]);
    }
    printf("%c]\n", a->array[i + 1]);
}

Could be:

void flexarray_print(flexarray *a){
    int i;
    printf("[");
    for(i = 0; i < a->size; i++){
        printf("%c,", a->array[i]);
    }
    printf("]\n" );
}

Additional stylistic hints: for sizes and indexes try to use unsigned types as much as you can.

struct buff {
     size_t size;
     size_t used;
     char *data;
     };

The advantage of this is that attempts to use negative indices ( b->data[b->used - 1] = item; ) will fail misarably and fast (instead of corrupting your memory slowly).

Also: you should test for the success/failure of realloc()

UPDATE2: it also appears there are some asterixes missing.

Sign up to request clarification or add additional context in comments.

Comments

1
flexarray *flexarray_new(){
    flexarray a;
    return a; // not a flexarray *
}

..and

a->array = (char*) erealloc (a ,a->capacity * sizeof(char)); 

What does 'erealloc()' do?

1 Comment

@wildplasser - there are quite a few to go at :)
0

your typedefs are wrong -- you either want to write

typedef struct flexarray { ... } flexarray;

and remove the typedef from the header, or

 struct flexarrayrec { ... }; // no typedef

and keep the tyepdef struct flexarrayrec flexarray; in the header.

1 Comment

Brilliant, this was one of the main things that confused me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.