1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
rawwar
  • 4,834
  • 9
  • 32
  • 57
  • 1
    `struct optional_tag { /*...* };` defines a struct. `a_type_alias { /*...*/ };` is not valid struct definition syntax. `struct coordinates { /*...*/ };` would define your structure. – Petr Skocik Sep 14 '19 at 12:55

4 Answers4

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

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;
stylo
  • 496
  • 4
  • 12
  • Sorry. No, i have a declaration `typedef struct corrdinates coordinates` in my header. without changing header, how can i make a struct coordinates in the main file – rawwar Sep 14 '19 at 12:35
  • if you're only referencing this struct from your main file, then you can just define the struct as I wrote but without the typedef keyword. and then declare it as I wrote. other than that, you cannot declare that struct outside of the main file unless you define it in another file. – stylo Sep 14 '19 at 12:40
  • @InAFlash: Does the header *not* also have a `struct coordinates { int x; int y; };`? – John Bode Sep 14 '19 at 12:41
  • No. It does not. @JohnBode. – rawwar Sep 14 '19 at 12:42
  • @InAFlash: is this code you wrote, or is this part of a library you are using? If the latter, it’s a fairly common practice to make types “opaque”, such that the details of the type are hidden (like the `FILE` type in `stdio.h`). Does the header provide an interface (function declarations) for setting or manipulating `coordinate` objects? – John Bode Sep 14 '19 at 12:47
  • @JohnBode, Thank you so much. I just read about making types opaque. Answers my question – rawwar Sep 14 '19 at 12:50
  • 1
    @InAFlash: Cool. Then all you need to do is declare on object of type `coordinates` and use the supplied methods to assign and manipulate it. – John Bode Sep 14 '19 at 14:57
0

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 !

ritesh sangani
  • 280
  • 3
  • 8