Use global Struct in other files

Hy guys,

I have a struct defined in one file:

struct.cpp

#include <stdio.h>

#include <stdio.h>

typedef struct _ZONA
{
int preco;
int numero;
int numLugaresVendidos;
int numLugares;
}ZONA;

ZONA _zona1;
ZONA _zona2;

How do i declare the _zona1 and _zona2 in other files to work with the same variables?

I know if i use a var like this:

int var;

to use in other file i do:

extern int var;

but i tried using:

extern ZONA _zona1;

but it give errors.

Thanks,

Daniel Gomes
You should declare the struct in a header file. Then include that header file within source files you want to use.

If you want to use the same instance, then you can use the extern keyword.
Thanks for your reply Zaita.

Zaita, can you show me a little example how to create the header file? I don't know how to create it.

Thanks,

Daniel Gomes

ZONA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef ZONA_H_
#define ZONA_H_

// Global Headers
#include <stdio.h>

typedef struct ZONA {
int preco;
int numero;
int numLugaresVendidos;
int numLugares;
};

#endif /* ZONA_H_ */ 


Now you can use
#include "ZONA.h"
Thanks Zaita, it works fine :)
Topic archived. No new replies allowed.