3
I have some files:
vetores.c // função main
uniao.c // função para unir vetores
ordena.c // função para ordenar vetores
globais.c // arquivo com variáveis globais
I want to know how I reference one file in the other.
Example: I need to use the variables of globais.c
in the vetores.c
, as well as calling the functions of ordena.c
and uniao.c
in the vetores.c
, and the functions of ordena.c
in uniao.c
and others... I tried using files headers (.h
) thus:
global. h
#ifndef _GLOBAIS_H_
#define _GLOBAIS_H_
#include<stdio.h>
#include<stdlib.h>
int *A, *B, ta, tb;
#endif
ordains. h
#ifndef _ORDENA_H_
#define _ORDENA_H_
int* ordena(int *vet, int tam);
#endif
union. h
#ifndef _UNIAO_H_
#define _UNIAO_H_
int* uniao( int *vet1, int tam1, int *vet2, int tam2);
#endif
And in the archives .c
I used references like this:
#include"uniao.h"
#include"globais.h"
#include"ordena.h"
I also tried straight through .c
:
#include"uniao.c"
#include"globais.c"
#include"ordena.c"
Okay, thanks for the explanation, it was really for educational questions, just to understand how it works etc, I’m more used to other languages. The global ones were created just to make this separation, because in the future I would need to indicate them in several places (the program will grow yet), I will return them as places.
– Leonardo
The more the program grows, the less it should use global variables. In fact, the ideal is that they do not exist in programs. I even understand the motivation, but it is no use doing something wrong to learn the right concept. If you have a better context, where separation is natural and necessary, it’s easier to show how. Other than that, it’s all right in the code, it should work (of course I didn’t see the rest).
– Maniero
In fact the global variables wouldn’t even need to exist in this case, just use pointer.. Thank you there.
– Leonardo
Yes, we can avoid global variables in 100% of cases.
– Maniero
In the case of the archives, I analyzed and could see the problem, I was including . h in wrong places, I did the following: I declared the only . h with the same name in . c and all . h in main, another error was in marriage. h not having included orneda.h.. I understood how it works.
– Leonardo