Array type has incomplete element type - c

I'm trying to do this:
typedef struct {
float x;
float y;
} coords;
struct coords texCoordinates[] = { {420, 120}, {420, 180}};
But the compiler won't let me. : ( What's wrong with this declaration? Thanks for your help!

Either do:
typedef struct {
float x;
float y;
} coords;
coords texCoordinates[] = { {420, 120}, {420, 180}};
OR
struct coords {
float x;
float y;
};
struct coords texCoordinates[] = { {420, 120}, {420, 180}};
In C, struct names reside in a different name space than typedefs.
Of course you can also use typedef struct coords { float x; float y; } coords; and use either struct coords or coords. In this case it won't matter what you choose, but for self-referencing structures you need a struct name:
struct list_node {
struct list_node* next; // reference this structure type - need struct name
void * val;
};

Related

Declaring object classes in C?

I declared a few geometric figures types such as:
typedef struct s_sphere{
t_tuple origin;
double radius;
} t_sphere;
typedef struct s_cylinder{
t_tuple origin;
double height;
double radius;
} t_cylinder;
typedef struct s_triangle{
t_tuple A;
t_tuple B;
t_tuple C;
} t_triangle;
etc...
Now, I would like to declare an intersection type which will contain two doubles and a geometric figure. I will then store all my intersections in a chained list:
// I do not know what type to give to geometric_figure
typedef struct s_intersection{
double t1;
double t2;
// what_type geometric_figure;
} t_intersection;
typedef struct s_intersection_list{
t_intersection intersection;
struct s_intersection_list *next;
} t_intersection_list;
I could use void* geometric_figure but I would like to avoid the most mallocs possible.
Is there a handy way of getting where I want without allocating geometric_object ?
type which will contain two doubles and a geometric figure.
Consider union with an identifier.
typedef struct s_intersection{
double t1;
double t2;
int id; // some id to know what type follows
union {
t_sphere sph;
t_cylinder cyl;
t_triangle tri;
} u;
} t_intersection;
If t_intersection is to be allocated, consider a flexible member array to right size the allocation.
typedef struct s_intersection{
double t1;
double t2;
int id; // some id to know what type follows
union {
t_sphere sph;
t_cylinder cyl;
t_triangle tri;
} u[]; // FAM
} t_intersection;
Example: allocate a triangle.
t_intersection *p = malloc(sizeof *p + sizeof p->u[0].tri);

Is it possible to do inheritance from an abstract/base struct or simulate something along those lines in C?

I am currently working with a C program that uses structs composed of xyz coordinates, but sometimes these coordinate may refer to vectors (Force/velocity type, not the data structure) while at other times it may be referring to position. I know its possible to simply use one struct for all of these different conditions since they all use mostly the same data type (float), but to simply keep my math better organized (plus variable and struct names) and keep things from getting mixed up, is there a way to define a base struct that defines itself as having three floats, be inherited by another struct that more specifically defines what the struct is supposed to be (such as position instead of velocity, etc. etc.)? I know C is not OOP, but it seems like it could be possible to do this Here is what the base struct would be like:
struct ThreeDCartesianData
{
float x;
float y;
float z;
};
A more specific struct would inherit from that and perhaps define extra variables, or use different names for the variables. There will be multiple position structs being used, but I think only one velocity struct for each set of data. I have seen similar questions to this, but they all seem to be referring to a higher level language (C++, C#, etc. etc.)
You can use a union for this. Your main struct would contain a union of the "derived" structs as well as a "flag" field telling you which member of the union is valid:
enum { DERIVED11, DERIVED2, DERIVED3 };
struct derived1 { int x1; };
struct derived2 { char x2; };
struct derived3 { float x3; };
struct ThreeDCartesianData
{
float x;
float y;
float z;
int derivedType;
union {
struct derived1 d1;
struct derived2 d2;
struct derived3 d3;
} derived;
};
Then you can use them like this:
struct ThreeDCartesianData data1;
data1.x=0;
data1.y=0;
data1.z=0;
data1.derivedType = DERIVED1;
data1.derived.d1.x1 = 4;
You could alternately define them like this:
struct common
{
int type;
float x;
float y;
float z;
};
struct derived1
{
int type;
float x;
float y;
float z;
int x1;
};
struct derived2
{
int type;
float x;
float y;
float z;
char x2;
};
struct derived3
{
int type;
float x;
float y;
float z;
float x3;
};
union ThreeDCartesianData {
struct common c;
struct derived1 d1;
struct derived2 d2;
struct derived3 d3;
};
And use them like this:
union ThreeDCartesianData data1;
data1.c.type=DERIVED1;
data1.d1.x=0;
data1.d1.y=0;
data1.d1.z=0;
data1.d1.x1 = 4;
If all the structs in a union have an initial list elements of the same type in the same order, the standard allows you to access those fields from any of the sub-structs safely.
What about using typedefs?
typedef struct general3d {
float x;
float y;
float z;
} general3d_t;
typedef general3d position;
typedef general3d velocity;
This way, when you come across something that's a velocity type, you encode that into the variable type, but under the hood, it's still just 3 points, x, y, z. Then, readers of the code will know you're talking about a velocity and not a position. If you want to get really crazy with it, you hide general3d away in some implementation file, so a user can never instantiate a general3d on their own, since they should be using either position or velocity as the situation requires; this may or may not be reasonable for your task at hand/worth the extra effort.
EDIT: I'm not positive about variable-renaming or about adding more variables directly to the same struct, but I would start to head in the direction of a different design at that point.
On the one hand, if you have two structs that have the same underlying types but require different names, you probably just have two separate structs. For example:
struct point3d {
float x;
float y;
float z;
};
struct person {
float age;
float weight;
float salary;
};
Yes, those are both 3 floats, but their understanding is very different, and they should be able to vary on their own if one or the other changes. Perhaps I want to add a name field to the person, but there's no reasonable analogue for a char * on point3d. Just define them separately if they mean different things.
As for adding more variables, that sounds like structs that contain other structs:
struct point3d {
float x;
float y;
float z;
};
struct person {
point3d position;
float age;
float weight;
float salary;
};
// access like:
person.position.x;
I've done this sort of thing before. I embed a copy of the base type at the front of the derived type struct. This more closely mimics what c++ might do. Here are two methods I've used.
Using simple type:
#define XYZDEF \
int type; \
float x; \
float y; \
float z
// base type
struct xyzdata {
XYZDEF;
};
// derived type 1
struct vector {
XYZDEF;
int vector_info;
...
};
// derived type 2
struct position {
XYZDEF;
int position_info;
...
};
#define BASEOF(_ptr) \
((struct xyzdata *) (_ptr))
// vector_rotate -- rotate a vector
void
vector_rotate(vector *ptr)
{
}
// position_rotate -- rotate a position
void
position_rotate(position *ptr)
{
}
// xyzrotate -- rotate
void
xyzrotate(xyzdata *ptr)
{
switch (ptr->type) {
case TYPE_POSITION:
vector_rotate((vector *) ptr);
break;
case TYPE_VECTOR:
position_rotate((position *) ptr);
break;
}
}
Using a virtual function table pointer:
#define XYZDEF \
int type; \
vtbl *vtbl; \
float x; \
float y; \
float z
// forward definitions
struct xyzdata;
struct vector;
struct position;
// virtual function table
struct vtbl {
void (*rotate)(struct xyzdata *);
};
// base type
struct xyzdata {
XYZDEF;
};
// derived type 1
struct vector {
XYZDEF;
int vector_info;
...
};
// derived type 2
struct position {
XYZDEF;
int position_info;
...
};
#define BASEOF(_ptr) \
((struct xyzdata *) (_ptr))
// vector_rotate -- rotate a vector
void
vector_rotate(struct xyzdata *ptr)
{
struct vector *vec = (void *) ptr;
...
}
// position_rotate -- rotate a position
void
position_rotate(struct xyzdata *ptr)
{
struct position *pos = (void *) ptr;
...
}
// xyzrotate -- rotate
void
xyzrotate(xyzdata *ptr)
{
ptr->vtbl->rotate(ptr);
}

Pointers Structure (C programming)

I have a struct that I don't understand :
typedef struct {
int x;
int y;
} Position;
typedef struct {
int id;
Position upper_left;
Position lower_right;
int priority;
} *Window_Description;
I don't understand why the struct *Window_Description has an asterisk before with? Is it a pointer to the structure? Because when I will create some Window_Description, it will be a pointer?
The definition
typedef struct {
int id;
Position upper_left;
Position lower_right;
int priority;
} *Window_Description;
is equal to
struct Window_Description_Struct
{
int id;
Position upper_left;
Position lower_right;
int priority;
};
typedef struct Window_Description_Struct *Window_Description;
That is, it makes Window_Description an alias for a pointer to the structure.

Assignment to pointer to structure from pointer within a different structure type

I am currently trying to assign a pointer to a structure called totheright (which is a linked list node). The pointer to the structure/list is currently within another structure called rectangles. I am encountering an error when I attempt to do so.
currect->right = malloc(sizeof(totheright));
righthead = current->right;
The following are the declarations in function:
rect *currect = NULL;
totheright *righthead = NULL;
Structure definitions in header:
typedef struct _totheright {
int rect;
struct _totheright *right;
} totheright;
typedef struct _rect {
int thisrect;
struct totheright *right;
int left;
double width;
double height;
double xcord;
double ycord;
struct _rect *next;
} rect;
Field right in structure rect should not have struct before totheright or struct _totheright should be used:
typedef struct _rect {
int thisrect;
//struct totheright *right; // was
totheright *right; // fixed
//struct _totheright *right; // that would be ok as well
int left;
double width;
double height;
double xcord;
double ycord;
struct _rect *next;
} rect;

C typedef function prototype with struct attempting to reference before defined.

I need to reference a struct that's not yet defined because the struct actually conatins the typedef'd function prototype.
For example,
typedef int (MyCallbackFunction)(X * x, void * ctx);
typedef struct CallbackData {
MyCallbackFunction * callback;
void * ctx;
} CallbackData;
typedef struct X {
char a;
int b;
int c;
double d;
CallbackData e;
} X;
What's the valid way to actually write this code/header ?
Just forward declare the relevant types - and you can make the function pointer part of the typedef:
struct X_;
typedef int (*MyCallbackFunction)(struct X_ * x, void * ctx);
typedef struct CallbackData_ {
MyCallbackFunction callback;
void * ctx;
} CallbackData;
typedef struct X_ {
char a;
int b;
int c;
double d;
CallbackData e;
} X;
Just forward declare your typedefs
typedef struct X X;
typedef struct CallbackData CallbackData;
and then declare the structs later.
Yes, you can forward declare the struct and use it in the declaration of MyCallbackFunction where you don't need it to be a complete type.
struct X;
typedef int (MyCallbackFunction)(struct X * x, void * ctx);

Resources