Passing a struct to function in diffrent file in c - c

I have two seperat files
in main function file 1:
struct entity{
unsigned int pos_x;
unsigned int pos_y;
unsigned int health;
} player;
struct inventory{
char helmet[length_item_names];
char body[length_item_names];
char legs[length_item_names];
char items[inventory_space];
} items;
render_screen(rendered_size_x, inventory_size_x, screen_size_y, map, player, items);
file 2:
void render_screen(int render_size_x, int inventory_size_x, int screen_size_y, char **field, struct entity player, struct inventory items){
...
}
when declaring render_screen i either get a error telling me that the struct entity and struct inventory can't be declared as incomplete data types or when i do
extern struct Player{
...
}
it tells me that the data type i give as a parameter is wrong
how can i give the struct as a prameter

The comment of Barmar workerd.
I had to put the declaration of the struct into a header file that i had to include in both c files.
Thanks for the quick help

Related

Array type has incomplete element type 'struct instruction'

How do I make this struct instruction visible to struct cpu state? Like If I put cpu state struct first it doesn't work because some other arguments won't be visible to struct instruction but if I put it reverse again I have a problem.
struct cpu_state
{
int nextinstructionlinenumber;
char filename[200];
char RLO;
struct instruction instructionlist[INSTRUCTIONLIST_SIZE];
int instructionlistnumber;
struct variable variablelist[INSTRUCTIONLIST_SIZE];
int variablelistnumber;
};
struct cpu_state CPU;
struct instruction{
char name[LINE_CHAR_NUM];
void(*function)(struct cpu_state* pstate, struct line* pline);
};
You can create incomplete structure declarations provided they are just used for pointers. For example, the following order will work. Note that I created a dummy definition for struct variable since it was absent from the post. You can replace it with whatever you like:
struct variable {
int dummy_val;
};
struct cpu_state;
struct line;
struct instruction{
char name[LINE_CHAR_NUM];
void(*function)(struct cpu_state* pstate, struct line* pline);
};
struct cpu_state
{
int nextinstructionlinenumber;
char filename[200];
char RLO;
struct instruction instructionlist[INSTRUCTIONLIST_SIZE];
int instructionlistnumber;
struct variable variablelist[INSTRUCTIONLIST_SIZE];
int variablelistnumber;
};
struct cpu_state CPU;

C - error: array type has incomplete element type in a extern struct declaration

I have next code:
// FILE: HEADERS.h
extern struct viaje viajes[];
extern struct cliente clientes[];
// FILE: STRUCTS.c
struct viaje {
char identificador[30+1];
char ciudadDestino[30+1];
char hotel[30+1];
int numeroNoches;
char tipoTransporte[30+1];
float precioAlojamiento;
float precioDesplazamiento;
};
struct cliente {
char dni[30+1];
char nombre[30+1];
char apellidos[30+1];
char direccion[30+1];
int totalViajes;
struct viaje viajes[50];
} clientes[20];
When I try to complile code I get next error: error: array type has incomplete element type in a extern struct declaration and I do not know why could it be. I have tried also include header after Structs definition and I do not get any error, but it is wrong, the correct way is Define -> Declare, not Declare -> Define.
Why could it be?. Thank you.
If you define or declare an instance of a struct, that struct needs to be defined first. Otherwise, the compiler can't figure out the size of the structure or what its members are.
You need to put the struct definitions first in your header file before the extern declarations:
struct viaje {
char identificador[30+1];
char ciudadDestino[30+1];
char hotel[30+1];
int numeroNoches;
char tipoTransporte[30+1];
float precioAlojamiento;
float precioDesplazamiento;
};
struct cliente {
char dni[30+1];
char nombre[30+1];
char apellidos[30+1];
char direccion[30+1];
int totalViajes;
struct viaje viajes[50];
}; // note that there are no instances defined here
extern struct viaje viajes[];
extern struct cliente clientes[];
Then your .c file would contain the instances:
// Changed size viajes from 20 to 50
struct viaje viajes[50];
struct cliente clientes[20];

Passing a struct item as the parameter

I understand it is possible to pass a struct as an argument etc.
But is it possible to have the parameter preset so only a specific struct item can be passed such:
struct inventory* searchForItem(struct stockItem.componentType code){
I'm getting : error: expected ';' , ', ' before token
EDIT:
typedef struct stockItem {
char *componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
(Since the comments on the other answer are too constrained)
First you define a new type for componentType, like this:
typedef char *stockItem_componentType; // Naming subject to conventions
Now in your structure, you use this type instead of simple char*. This is optional, but very much recommended. Like this:
typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
And finally, your function prototype is:
struct inventory* searchForItem(stockItem_componentType code);
The type of that component is char *, so just make that the type of the parameter.
struct inventory* searchForItem(char *code){
If you want to make the type more strict, make a typedef for the field in question:
typedef char * stockItem_componentType;
typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
struct inventory* searchForItem(stockItem_componentType code){
Note that this hides a pointer behind a typedef which is not recommended. Then people reading your code (including yourself) won't know just by looking at it that it's a pointer, which can lead to confusion.

different formats to represent the structures

I have watched videos by different people on youtube and now i am little bit confused
first two[(1) (2)] are of same type without typedef and then two[(3) (4)] are of same type with typedef but the way of writing them is different
Please mention if any of the below represented structure is wrong and why with number. I am assigning number to all of the these formats
(1)
struct tag{
int number;
char *name;
float num;
};
main()
{
struct tag name;
name firstmember;
------code-------
return 0;
}
Next type i have seen is same as the above but :
(2)
struct tag{
int number;
char *name;
float num;
}name;
main()
{
name firstmember;
------code-------
return 0;
}
Next is using structure with typedef:
(3)
typedef struct tag{
int number;
char *name;
float num;
}name;
main()
{
name firstmember;
------code-------
return 0;
}
same version of typedef with other one:
(4)
struct tag{
int number;
char *name;
float num;
};
main()
{
typedef struct tag name;
name firstmember;
------code-------
return 0;
}
Please mention if any of the the above represented examples have any kind of error
Lastly this doubt is completely different from the above examples
if we don't put a structure tag just after the word struct and do like this
(5)
struct {
int number;
char *name;
float num;
}tag;
main(){}
does this word 'tag' remains structure tag or if its after '}' so its a structure name now ? What is it? If it becomes the name why do we use the structure tags anyways?
Please respond to this question after reading all my doubts carefully in well written format
Have a look at these close threads with popular answers:
Difference between 'struct' and 'typedef struct' in C++?
typedef struct vs struct definitions
Why should we typedef a struct so often in C?
struct and typedef in C versus C++

struct array inside another struct

I'm trying to create an structure with other structures inside.
struct bullet{
char bullet_sprite[100];
int pos_x;
int pos_y;
int ace_x;
int tag;
};
struct bullets_onscreen{
struct bullet v[2];
struct bullet a[2];
};
I get this error:
error: array type has incomplete element type
Is this posible to do?
Example code:
//Calling functions
struct bullets_onscreen[2] //public
struct bullet bala[1];
init_bullet(&bala,_player);
set_bullet_on_screen(&bala);
void set_bullet_on_screen(struct bullet *_bullet){
array_bullet[1] = _bullet;
}
void init_bullet(struct bullet *_bullet, struct player *_player){
//inits all bullet components
}
As written your code is fine. Presumably in the actual code you have reversed the order of the two struct definitions. This code produces the error you report:
struct bullets_onscreen{
struct bullet v[2];
struct bullet a[2];
};
struct bullet{
char bullet_sprite[100];
int pos_x;
int pos_y;
int ace_x;
int tag;
};
Define the structs in the order that you did in the question and your code will compile.

Resources