One problem is the calls to malloc()
and the fact that you've not declared malloc()
by including <stdlib.h>
.
By default, functions are assumed to return an int
in pre-C99 code — in C99 code, you're supposed to declare a function before using it.
You need to compile with more warning options. If you use GCC, I recommend:
gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition ...
This pretty much ensures that you don't have undeclared functions (such as malloc()
) used. Depending on the version of GCC you use, you may get more or less warnings enabled by default. In general, newer versions are fussier, though it isn't quite that simple.
The other problem appears to be that you have a source file (name not given in the question) containing type definitions and function definitions such as:
typedef struct TipoPagina* TipoApontador;
typedef struct TipoPagina { ... } TipoPagina;
TipoApontador NovaSubArvore(int ordem) { ... }
Within this file the types are known. In your main code, you have:
TipoApontador Raiz;
...
Raiz = NovaSubArvore(ordem); //Warning happens here
The type name TipoApontador
must be known in this file, but it appears that your code does not include a declaration for NovaSubArvore()
.
For types and functions that are going to be used in multiple source files, there should be a header defining the types and declaring the functions. The header should be used in both the source file that defines the functions and in the source files that use the types and functions.
For example, the header might be tipopagina.h
:
#ifndef TIPOPAGINA_H_INCLUDED
#define TIPOPAGINA_H_INCLUDED
typedef int TipoChave;
typedef struct TipoRegistro {
TipoChave Chave;
/*outros componentes*/
} TipoRegistro;
typedef struct TipoPagina* TipoApontador;
typedef struct TipoPagina {
int registros;
TipoRegistro *r;
TipoApontador *p;
} TipoPagina;
extern TipoApontador NovaSubArvore(int ordem);
#endif /* TIPOPAGINA_H_INCLUDED */
The header guards are important; they avoid problems with redefining types (though C11 has more flexibility than either C99 or C89 in handling redefinitions of typedef
s). The use of extern
before the function name is not strictly necessary, though I prefer to see it — if only for symmetry with the extern
that must be present before any variables that are declared in the header (if there are any — global variables should be avoided whenever possible).
Then the implementation file tipopagina.c
might start:
#include "tipopagina.h"
#include <stdlib.h>
TipoApontador NovaSubArvore(int ordem)
{
TipoApontador A = malloc(sizeof(TipoPagina));
...
return (A);
}
There's a good reason for putting the tipopagina.h
header first; it ensures that the header can be used on its own (which is important).
The main code also includes tipopagina.h
, and because the function NovaSubArvore()
is declared in the header, you avoid the compiler warning.