0

I'm trying to enter a value into the struct I have in my header file. The value is given in the main file and is passed as an argument to a function. But I'm getting an error that says error: dereferencing pointer to incomplete type ‘struct Array’ array->size = size; The compiler is talking about the arrow in the line array->size = size; I have provided my files below.

Header.h:

#include <stdio.h>
//Include libraries used

struct Array {
  unsigned int size;
  unsigned int cap;
  int data;
};
//Declare struct Array

struct Array *newArray(unsigned int size, unsigned int cap);

function.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct Array *newArray(unsigned int size, unsigned int capacity ) {

  struct Array *array;

  array->size = size;

}

My main.c file has included the header file so I don't have it in my function.c file. It also calls the function newArray(5, 20), giving size the value of 5. How do I fix this error so I can have size in the struct equal to 5? Thanks, I would really appreciate the help.

3
  • You need to set array before attempting to dereference it. It's an undefined pointer value. Commented Oct 5, 2021 at 15:25
  • Furthermore, struct Array does not have member width. Don't be sloppy, but also post the exact code you are trying, don't show us errors that don't apply to the code you post. Commented Oct 5, 2021 at 15:36
  • @Cheatah I fixed the code, my apologies I entered the information incorrectly. Commented Oct 5, 2021 at 17:05

1 Answer 1

2

You need to include the header Header.h where the structure is defined in the module function.c where the function is defined.

Otherwise in this function definition

struct Array *newArray(unsigned int width, unsigned int capacity ) {

  struct Array *array;

  array->width = width;

}

there is introduced the incomplete type specifier struct Array. And the compiler that compiles this module does not know whether the structure contains such a data members as width.

Though it seems your code snippet contains a typo because the structure definition does not contain the data member width and the compiler in the message refers to the data member size.

Also pay attention to that the pointer array is not initialized.

struct Array *array;

and has an indeterminate value.

So in any case even if you will include the structure definition this statement

array->width = width;

will invoke undefined behavior.

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

2 Comments

I fixed my code , I had entered 'width', when it should have been 'size'
@Raswall It is not very important. I already explained the reason of the error message.

Your Answer

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