0

I'm trying to implement a dynamic array with code suggested on this thread C dynamically growing array . I want to return a struct Array from a function, but I get an error on the first line of foo() (error expected expression before 'struct'). I've seen many other examples of this, but the solutions don't seem to apply. My apologies if one of them does. Here is my full code:

typedef struct {
  int *array;
  size_t used;
  size_t size;
} Array;

void initArray(Array *a, size_t initialSize) {
  a->array = (int *)malloc(initialSize * sizeof(int));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, int element) {
  // a->used is the number of used entries, because a->array[a->used++] 
//updates a->used only *after* the array has been accessed.
  // Therefore a->used can go up to a->size
  if (a->used == a->size) {
    a->size *= 2;
    a->array = (int *)realloc(a->array, a->size * sizeof(int));
  }
  a->array[a->used++] = element;
}

void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = a->size = 0;
}

struct Array foo ()
{
    Array a;
    int i;
    initArray(&a, 5);  // initially 5 elements
    for (i = 0; i < 100; i++)
      insertArray(&a, i);  // automatically resizes as necessary
    printf("%d\n", a.array[9]);  // print 10th element
    printf("%d\n", a.used);  // print number of elements
    freeArray(&a);
    return struct Array a;
}
  • The `typedef` allows you to just say `Array`, saving you from having to always say `struct Array`, so why not be consistent with it? – Mike Nakis Aug 05 '17 at 15:53
  • `struct Array foo ()` ==> `Array foo ()` because there is no `struct Array`. Similarly with `return struct Array a;` ==> `return a;` but worse, you seem to be returning an entity you just `free`d? – Weather Vane Aug 05 '17 at 15:54
  • I wanna add that there is a difference between how `c` treats structs and how `c++` does it. In `c++` you can use `struct tag_name` and `tag_name` interchangeably but you can't do it in `c`, so be careful with code samples that you might find online. – m0h4mm4d Aug 05 '17 at 16:02

1 Answers1

0

The first issue I see is that you are specifying a type on the last line

return struct Array a;

should be just

return a;

Another issue however is that you are not dynamically allocating the array and returning a pointer, so after this function is finished the value of a could be overwritten because it is allocated on the stack. Try this:

Array* foo() {
    Array *a = malloc(sizeof(Array));
    int i;
    initArray(a, 5);  // initially 5 elements
    for (i = 0; i < 100; i++)
        insertArray(a, i);  // automatically resizes as necessary
    printf("%d\n", a->array[9]);  // print 10th element
    printf("%d\n", a->used);  // print number of elements
    freeArray(a);
    return a;
}
Anthony Palumbo
  • 314
  • 1
  • 11
  • Also you may need to change your implementations of `initArray`, `insertArray`, and `freeArray`. – Anthony Palumbo Aug 05 '17 at 16:00
  • There's no problem with returning a `struct`. I don't see the original code returning a *pointer* to it. –  Aug 05 '17 at 16:29
  • It's generally better to return a pointer because then only 8 bytes needs to be transferred, when returning a struct without a pointer, it must transfer over all the data. – Anthony Palumbo Aug 05 '17 at 16:31
  • this is debatable, but you write OP was returning a pointer to a, while he doesn't, so that's not a problem. –  Aug 05 '17 at 16:42