Niku
Niku

Reputation: 3

C typedef struct redefinition, different basic types

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

Answers (4)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181290

You need:

#include "person.h"
person persondata[10];

Upvotes: 0

davmac
davmac

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

onitake
onitake

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

Oliver Charlesworth
Oliver Charlesworth

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

Related Questions