Error calling functions using C structures

3

I am very confused in this code. I get confused because if I declare my functions first before the structures the error that appears to me is

: 6: 13: error: unknown type name 'patient'

Putting the functions before the structures.

#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>

int menu();
void datos (paciente pac[]);

typedef struct signos_vitales
{
    int glucosa;
    float temperatura;
    float presion_d;
    float presion_s;
    int pulso;
}signos_vitales;

typedef struct paciente
{
    char paterno[50];
    char materno[50];
    char nombre[50];
    int edad;
    char sexo;
    bool asegurado;
    signos_vitales signos;
}paciente;

main()
{
    int num, m, seg, hiper, hipo;

    printf("Cuantos pacientes quieres registras\n");
        scanf("%d", &num);

    paciente pac[num];

    m = menu();

    switch(m)
    {
        case 1:
            datos(pac);
            break;
    }
}

But if I change the order and put the functions after the structures, when compiling, the error changes and this appears to me.

In function main:

hospital.c :(. text + 0x87): undefined reference to menu '

:( hospital.c text + 0x9E). Undefined reference to data '

collect2: error: ld returned 1 exit status

What am I doing wrong?

    
asked by Diego 04.04.2016 в 08:24
source

1 answer

2

During the compilation phase the compiler (worth the redundancy), processes the different code files generating a sort of object code. In order to generate that object code you need to know certain details of the types and functions used to prepare the calls correctly.

In the first case that you comment, "put the functions first and then the structures", the problem that the compiler finds is that the functions make use of the structures and, of course, the compiler totally ignores the meaning of paciente . Is it an alias? Is it a structure? A pointer to function? A macro? As you do not know it, it shows you an error, it aborts and it stays so comfortable. It is for this reason that the usual thing is to put the structures at the beginning and then the functions.

In the second case, what seems to be happening is that the compiler has finished the compilation phase correctly, however in the linkage phase it has not been able to find the implementation of menu() . Since in your example you have not put the implementation of this function the obvious question is do you have the implementation of that function somewhere?

If the answer is yes, then it is clear that it is not in the same file as the function main() , you need an include so that the linker can do its work.

If the answer is no ... you have to implement the function to use it.

(And the same for datos() )

Greetings.

    
answered by 04.04.2016 в 11:08