Reputation: 3
First time posting here. I have problem about referencing a typedef struct from separate files. Example:
main.c //menus and variables declaration
#include <stdio.h>
#include <string.h>
#include "person.h"
#include "person.c"
person persondata[50] ;
person.h //typedef struct{...}person;
typedef struct
{
char name[50];
}person;
person.c //functions
extern persondata[];
void copy()
{
strcpy(persondata[0].name, "John");
}
I keep getting error: left of ".name" must have struct/union type and redefinition, different basic types
How am I supposed to fix this reference?
Upvotes: 0
Views: 3225
Reputation: 20631
Typically you would #include "person.h"
so as to make the declaration of person
visible, and then you would change the persondata declaration to:
extern person persondata[];
... i.e. specify its type.
Upvotes: 2
Reputation: 1409
Write
extern person persondata[];
instead. This should do the trick.
If you don't give it the correct type, the compiler won't know that persondata is an array of persons.
Upvotes: 1
Reputation: 272517
extern persondata[];
is implicitly equivalent to extern int persondata[];
(the compiler assumes int
if you don't specify a type). You need extern person persondata[];
.
Also, the compiler needs to be able to see the definition of person
in person.c, so add #include "person.h"
at the top of the file.
Upvotes: 1