Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef struct unknown type name [closed]

After searching for a while. I code a simple program to try and solve my bigger problem easier having little or no success. This is the code which works fine in main:

typedef struct nimaginario{
    int parte_real;
    int parte_imaginaria;
}nimaginario;

int suma(nimaginario *n){
    return n->parte_real + n->parte_imaginaria;
};

#include <stdlib.h>

int main(){

    nimaginario *num = malloc(sizeof(nimaginario));
    num->parte_real = 1;
    num->parte_imaginaria = 2;
    int a = suma(num);
}

Then I try dividing it into the different parts it should be organized in. As to say: prueba.c, prueba.h, and main.c

prueba.h:

#define PRUEBA_H
#ifndef PRUEBA_H

typedef struct nimaginario{
    int parte_real;
    int parte_imaginaria;
}nimaginario;

int suma(nimaginario *n);

#endif

prueba.c:

#include "prueba.h"

int suma(nimaginario *n){
    return n->parte_real + n->parte_imaginaria;
};

main.c:

#include <stdlib.h>
#include "prueba.h"

int main(){

    nimaginario *num = malloc(sizeof(nimaginario));
    num->parte_real = 1;
    num->parte_imaginaria = 2;
    int a = suma(num);
}

In the first place I compiled with the command 'gcc main.c' In second place I did as follows: 'gcc prueba.c main.c'

and for this second time and this specific try (out of one hundred) I get this errors:

prueba.c:3:10: error: unknown type name `nimaginario'
main.c: In function `main':
main.c:18:2: error: unknown type name `nimaginario'
main.c:18:35: error: `nimaginario' undeclared (first use in this function)
main.c:18:35: note: each undeclared identifier is reported only once for each function it appears in
main.c:19:5: error: request for member `parte_real' in something not a structure or union
main.c:20:5: error: request for member `parte_imaginaria' in something not a structure or union

I think I have tried everything and anything. And seen all the errors I could see. I've also googled around a lot, and searched here in Stackoverflow with no success what so ever.

Things I've tried:

  • Changing the typedef structure... and doing it step by step.
    • doing first typedef, then struct and vice versa.
  • Changing the names of labels in struct and typedef.
  • Checking #define /#ifndef
  • Changing pointers all over (originally I wanted the struct to be to integer pointers) forgetting about typedef and working only with struct. Therefore declaring always as struct nimaginario.
  • Checked where my files are.

I just fail to have a hammer close. I feel so numb It has just been a year without C...

like image 642
MikeMajara Avatar asked Dec 20 '22 04:12

MikeMajara


1 Answers

You have an issue with the include guards ;-)

You want to check first then define it, not the other way around.

#define PRUEBA_H
#ifndef PRUEBA_H
...

should be

#ifndef PRUEBA_H
#define PRUEBA_H
....
like image 181
P.P Avatar answered Dec 22 '22 01:12

P.P