0

I am trying to send array (which is an object?) that contains random integers. I want to send the array to sort() in order to sort the array, but when I am parsing array to sort() I get the following error: error: dereferencing pointer to incomplete type. Please, if you are changing anything in the code, please please explain what you are doing and why you are doing it because I am already confused about the code as it is right now.

I am not allowed to change the code in array.c or array.h, and void_sort()'s argument can not be changed.

array.c

struct array {
    int* data;
    int size;
};

struct array* array_create()
{
    struct array* array = (struct array*) malloc(sizeof(struct array));
    array->data = (int*) malloc(sizeof(int) * 10000);
    array->size = 10000;
    return array;
}

array.h

typedef struct array* ARRAY;

ARRAY array_create();

main.c

void sort (int A[], int N) 
{
    // Sort the array
}

ARRAY array;
array = array_create();
sort(array->data, 100);  // This gives me error: dereferencing pointer to incomplete type
user3285214
  • 21
  • 1
  • 5
  • If `sort()` doesn't see the full (complete) definition of `struct array`, then you can't do this. Also, [do ***NOT*** cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)! –  Feb 09 '14 at 13:34
  • @mah No, that's not what the problem is. –  Feb 09 '14 at 13:35
  • @mah Hmm, the thing is that I have a header file called **sort.h** and it contains `void sort (int* A, int N);`. The `void sort (int A[], int N) ` (that you can see in **main.c**) is actually in an own file called **sort.c**. I am so confused because I can't see that **sort.h** is calling **sort.c** anywhee....... – user3285214 Feb 09 '14 at 13:37

2 Answers2

1

You need to move the declaration of the array struct to array.h. The error you are getting is because that type is not known in main.c. You can use pointers to an undefined type, but you cannot access its members.

If you want to keep the implementation hidden, you can create access functions. Add to array.h:

int *array_data(ARRAY array);
int array_size(ARRAY array);

and to array.c:

int *array_data(ARRAY array) { return array->data; }
int array_size(ARRAY array) { return array->size; }

and in main.c:

sort(array_data(array), array_size(array));
C. E. Gesser
  • 811
  • 6
  • 12
  • Ok, but why can I not just pass the whole **array** to **sort()**? So instead of typing `sort(array->data, 100);` I am typing `sort(array, 100);`? – user3285214 Feb 09 '14 at 13:42
  • I still think this has something to do with this: Hmm, the thing is that I have a header file called **sort.h** and it contains `void sort (int* A, int N);`. The `void sort (int A[], int N) ` (that you can see in **main.c**) is actually in an own file called **sort.c**. I am so confused because I can't see that **sort.h** is calling **sort.c** anywhere... – user3285214 Feb 09 '14 at 13:44
  • ^Thus because when I am calling `void sort(array->data, 100);` I get the error "Undefined reference to sort". – user3285214 Feb 09 '14 at 13:46
  • @user3285214 but then that's not a compiler error, that's a linker error, and it has nothing to do with your header files. –  Feb 09 '14 at 13:52
  • That *is* a compiler error. A linker error would be something like "undefined reference to something". The error here occurs because the compiler only knows ARRAY is a pointer to some struct (in main.cpp), and it cannot know what is *inside* that struct. – C. E. Gesser Feb 09 '14 at 14:01
  • @C Yes it is working now because I put the struct in array.h. But I get the "undefined reference to sort" when I am typing `sort(array->data, 100);`. This is because there is nothing that links sort.h and sort.c together. Sort.h contains `void sort (int* A, int N);` and sort.c contains `void sort (int A[], int N);`. – user3285214 Feb 09 '14 at 14:04
  • Ok. If you have `void sort (int* A, int N);` in sort.h, you need `void sort (int* A, int N) { /* sorting logic */ }` in sort.c. – C. E. Gesser Feb 09 '14 at 14:07
  • And of course you need to compile all three ".c" files together. Something like `gcc array.c sort.c main.c`. – C. E. Gesser Feb 09 '14 at 14:09
  • @C Ah yes, I didn't compile the sort.c file, heh... But I still don't understand how **sort.h** and **sort.c** gets connected. No one includes each other? :S – user3285214 Feb 09 '14 at 14:23
  • The compiler only needs to know that the function exists, its name and parameters, when it generates code for the invocation in main.c. And the linker later finds the function implementation. – C. E. Gesser Feb 09 '14 at 14:31
1

You need to move this from array.c to array.h:

struct array {
    int* data;
    int size;
};

That's because this declares the array type. If you don't want to do that, you can copy-paste it into main.c, and everyone will hate you but you'll accomplish your objective of not modifying array.h or array.c.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436