Juan Bravo
Juan Bravo

Reputation: 33

Defining a struct function

I have the next code, but qhen I compiled, it appear an erro: error: expected identifier or '(' before 'float'. But it is just exacly as other code i have and examples from Internet but I don't notice the error.

#ifndef Deteccion
#define Deteccion
#define acc_TH 12.74
int cont=0;
int pasos=0;
float SF,SL,SLT;

struct Deteccion
{
int muestras;
int pasos;
float frecuencia;
float longitud;
float orientacion;
};

struct Deteccion Detectar(float ax, float ay, float az, float wz)
{
float mag,longitud,frecuencia;
int muestras;
float sum_acc,sum_w,orientacion;
mag=(ax*ax)+(ay*ay)+(az*az);
mag=pow(mag,0.5); 
if(mag>=acc_TH)
{
    muestras=muestras+1;
    sum_acc=mag+sum_acc;
    sum_w=wz+sum_w; 
}
else
{
    cont=cont+1; 
    if(muestras<32)
    {   
        muestras=0;
        sum_acc=0;
        sum_w=0;
    }
}
if(cont==4)
{
    pasos=pasos+1;
    SF=50/muestras;
    SL=sum_acc/muestras;
    SL=pow(SL,0.333);
    SL=0.22*SL;
    SLT=SF+SL;
    orientacion=(0.02*sum_w)+orientacion;
    sum_w=0;
    muestras=0;
    sum_acc=0;
}
}
#endif

Upvotes: 0

Views: 80

Answers (3)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140168

Problem is you have defined Deteccion as an empty macro first, which explains the compilation error. The compiler sees struct Detectar(float which is illegal.

Funny enough, struct Deteccion definition works, just seen as an anonymous structure declaration. Although it is useless because not recallable.

General advice: protect against multiple inclusion with that technique only in header files. It is useless in .c files, And use a distinctive naming rule like uppercase plus prefix.

#ifndef DETECCION_H
#define DETECCION_H

As a convention, macros are generally full uppercase, which avoids problems like you had.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Since you are using Deteccion as the header guard, you may not use the same identifier for the struct tag. Preprocessor replaces Deteccion with blank space, so the compiler sees this:

struct // <<== Nothing
{
    int muestras;
    int pasos;
    float frecuencia;
    float longitud;
    float orientacion;
};

To fix this problem use a different identifier for the header guard:

#ifndef Deteccion_H
#define Deteccion_H

Note: It looks like you are using header guard inside a file that has function implementation. This is highly unusual. Consider restructuring your code so that implementations and declarations are placed in different files.

Upvotes: 4

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13171

Because you have:

#define Deteccion

defining "Deteccion" to be nothing, the line

struct Deteccion Detectar(float ...

is actually

struct Detectar(float ...

which is an error.

Upvotes: 2

Related Questions