TheCrifix TheCrifix -4 years ago 192
C Question

Dereferencing pointer to incomplete type 'struct ____'

I am attempting to create a type in C that can accept most primitive types. I am new to C and don't understand structs very well. My error occurs on line 10 (of main.c) and it will also occur on line 11 if line 10 is removed (also of main.c). If anyone has an ideas/pointers I would really appreciate it! Many thanks!

main.c:

#include <stdio.h>

#include "modularType.h"

int main()
{
int data = 1;

PyType object = createPyObjectInt(data);
printf("Type of data: %d\n", object->typeOfData);
printf("Value of data: %d\n", object->intData);

return 0;
}


modularType.h:

typedef struct pytype *PyType;

PyType createPyObjectEmpty(void);
PyType createPyObjectInt(int data);

void setDataInt(PyType, int data);
void setDataIntStar(PyType, int* data);

void freeMyType(PyType);
void freeCharStar(PyType);
void freeIntStar(PyType);
void freeFloatStar(PyType);


modularType.c:

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

#ifndef NEW_TYPE
#define NEW_TYPE

#include "modularType.h"

typedef enum
{
NO_DATA,
REG_CHAR,
CHAR_STAR,
REG_INT,
INT_STAR,
REG_FLOAT,
FLOAT_STAR,
}types;

struct pytype
{
//The number of data types i can hold here
unsigned char typeOfData: 3;

union
{
char charData;
char* charStarData;
int intData;
int* intStarData;
float floatData;
float* floatStarData;
}
};

PyType createPyObjectEmpty(void)
{
PyType object;

object = malloc(sizeof(*object));

object->typeOfData = NO_DATA;

return object;
}

PyType createPyObjectInt(int data)
{
PyType object;

object = malloc(sizeof(*object));

object->intData = data;
object->typeOfData = REG_INT;

return object;
}

void setDataInt(PyType object, int data)
{
object->intData = data;
object->typeOfData = REG_INT;
}

void setDataIntStar(PyType object, int* data)
{
object->intStarData = data;
object->typeOfData = INT_STAR;
}

#endif


As a side note my compilation command (gcc -Wall -std=c99 -o modType main.c modularType.c) produces the following warning: modularType.c:35:1: warning: no semicolon at end of struct or union. I thought that I had formatted my struct correctly but I have also seen people define structs as following:

typedef struct pytype
{
//code here
}PyType;


Is this a better way or is the way I am doing it fine?

Answer Source

The structure definition is incomplete in the header file, so you cannot refer to any struct members in your code. The compiler complains that it does not know about any struct members at all.

An incomplete structure definition is one where you do not provide the list of members for the implementation. Such a definition allows you to manipulate pointers to such structures, but not access any members since they are not explicitly defined and the compiler needs to know their type and offset from the beginning of the structure to generate the corresponding code.

Also not that the typedef PyType hides a pointer to a struct pytype. It is error prone to hide pointers behind typedefs, leading to confusing code for both the programmer and the reader.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download