Reputation: 4992
I have the following in the header file named vector.h
typedef struct coordinates coordinates;
Coordinates struct should have two variables. x
and y
How can I include these two variables x
and y
without changing anything in header file?
my idea was to write the following in main.c
coordinates{
int x;
int y;
};
I wrote above because I already wrote a typedef struct coordinates
in vector.h. So, if I write again, it is repeated. But the above syntax itself is wrong as compiler is throwing error. please help me if I understood structures wrong or help with how to declare variables inside the struct.
Upvotes: 1
Views: 1903
Reputation: 340
typedef struct coordinates coordinates;
is incomplete declaration and hence the compiler will throw an error, when you try to declare the members of struct in main.c file.
coordinates{
int x;
int y;
};
The error thrown will be expected identifier. The complier is looking for a declaration but it is not able to find it.
You can declare the coordinates struct like this in main.c
typedef struct coordinates{
int x;
int y;
}coordinates;
and then you can use it as type. I would not recommend doing that because it is a repeated exercise and not a good programming practice.
The best way is to declare the struct in your vector.h file like this:
typedef struct coordinates{
int x;
int y;
}coordinates;
and then use it in your main.c file
Hope this helps !
Upvotes: 0
Reputation: 310930
This declaration in the header
typedef struct coordinates coordinates;
is not very useful because usually a complete structure definition is required in most cases. So in general it is better to append the header with the complete structure definition.
For example
typedef struct coordinates coordinates;
struct coordinates
{
int x;
int y;
};
The single typedef declaration is enough only in cases when there are not required the compete type of the structure. For example when only pointers to objects of the structure are declared.
If you may not change the header then include this definition
struct coordinates
{
int x;
int y;
};
in a module where the structure is referenced.
Upvotes: 1
Reputation: 180103
I have the following in the header file named vector.h
typedef struct coordinates coordinates;
That's a declaration of the identifier coordinates
as an alias for a type struct coordinates
, which itself is an "incomplete type" as far as that declaration is concerned.
How can I include these two variables x and y without changing anything in header file?
It is struct coordinates
that needs to be "completed" with a definition before you can access members:
struct coordinates {
int x;
int y;
};
Such a definition needs to be in scope wherever you access the members of an instance of that type, whether by the type name struct coordinates
or by its alias coordinates
. It is conventional to put such a definition in a header file, so that it is appropriately shared among translation units, but if you need access to the members (or the overall size of the structure) only in one file, then you can instead put the type definition above only in that file. Alternatively, you can duplicate the definition identically in every translation unit that wants to access the members, but that's poor form and hard to maintain.
Upvotes: 1
Reputation: 506
I'm assuming you mean
typedef struct coordinates
{
int x;
int y;
} coordinates;
then just create a struct with the following line
coordinates c;
c.x = 0;
c.y = 1;
Upvotes: 0