1

I have some problems passing my enum pointer to a function parameters.

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

//Prototypes
void automate(int tddv_estime, int tab_para_automate[][3], enum* Etat);
void fct_test(int scenario[]);


/*-----------------------------------------------------------------------
main()
-------------------------------------------------------------------------*/
int main()
{ 
int scenario[15] = {21, 21, 20, 12, 12, 20, 22, 22, 22, 22, 22, 22, 22, 22, 500};

fct_test(scenario);
return 0;
}

/*-----------------------------------------------------------------------
Fonction fct_test
-------------------------------------------------------------------------*/
void fct_test(int scenario[])
{
    //PARAMETRES
    int increment = 1;
    int tddv_estime;


int para_1_min = 5;
int para_1_max = 10;

int para_2_min = 1;
int para_2_max = 4;

int tab_para_automate[2][3] = {{0}};
int tab_para_application[2][2] = {{0}};

int i,j,k;

enum Etat {INIT, DECREMENT_PARA_1, INCREMENT_PARA_1, ETAT_INVARIANT_1, DECREMENT_PARA_2, ETAT_INVARIANT_2, INCREMENT_PARA_2, RADAR_SATURE};

/*printf("ETAT INITIAL\n");
printf("Para_1_courant = %d\n" , para_1_courant);
printf("Para_2_courant = %d\n\n\n" , para_2_courant);*/

for (k=0 ; k<16 ;k++)
{
    tab_para_automate[k][0] = para_1_min;
    tab_para_automate[k][1] = para_1_max;
}


for (j=1 ; j<16 ;j++)
{
    tab_para_automate[j][0] = para_2_min;
    tab_para_automate[j][1] = para_2_max;
}


    do {
    for (i=0 ; i<16 ; i++)
   {
        tddv_estime = scenario[i];
        automate(tddv_estime, tab_para_automate, &Etat);
   }
        }while (scenario[i] != 500);
}

/*-----------------------------------------------------------------------
Fonction automate
-------------------------------------------------------------------------*/

void automate(int tddv_estime, int tab_para_automate[][3], enum* Etat)
{
int evenement;

int tddv_worst = 20;
int para_1_min = tab_para_automate[0][0];
int para_1_max = tab_para_automate[0][1];

int para_2_min = tab_para_automate[1][0];
int para_2_max = tab_para_automate[1][1];

int para_2_courant = para_2_max;
int para_1_courant = para_2_max;

if (tddv_estime < tddv_worst)
    evenement = 1; //Etat initial

if (tddv_estime > tddv_worst)
    evenement = 2; //Decrement para1

if (tddv_estime < tddv_worst && tab_para_automate[0][0] < tab_para_automate[0][1])
    evenement = 3; //Increment para1

if (tddv_estime == tddv_worst)
    evenement = 4; //Etat Invariant 1

if (tddv_estime > tddv_worst && tab_para_automate[0][0] <= tab_para_automate[0][0])
    evenement = 5; //Decrement para_2

if (tddv_estime < tddv_worst && para_2_courant < tab_para_automate[1][1])
    evenement = 6; //Increment para2

if (tddv_estime == tddv_worst)
    evenement = 7; //Etat Invariant 2

if (tddv_estime > tddv_worst && tab_para_automate[0][0] <= para_1_min && para_2_courant <= para_2_min)
    evenement = 8; //Etat radar sature


}

I can't pass my enum pointer in parameters, i don't know why. I've been looking for the solution for a while now, can someone help me on this please ? Maybe the problem is in the prototypes, maybe i don't declare well my enum variable, i can't find the solution.

5
  • 1
    You already seem to understand the problem... Move the enum definition out of the function, before your prototypes. Commented Jul 4, 2018 at 13:39
  • In this line: automate(tddv_estime, tab_para_automate, &Etat); what is Etat? there is no such symbol in the scope of fct_test? Commented Jul 4, 2018 at 13:49
  • I moved the enum definition before the prototypes and i added : Etat etat = INIT; to instanciate the variables but i still have problems... Commented Jul 4, 2018 at 13:49
  • I still have problems: that is...? Commented Jul 4, 2018 at 13:50
  • Note: 16 > 15 Commented Jul 4, 2018 at 14:07

1 Answer 1

1

You have to declare the type enum Etat before the first usage (i.e. directly after your includes):

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

enum Etat {INIT, DECREMENT_PARA_1, INCREMENT_PARA_1, ETAT_INVARIANT_1, DECREMENT_PARA_2, ETAT_INVARIANT_2, INCREMENT_PARA_2, RADAR_SATURE};

and fix the prototype and definition to use the new type (remember: the type is enum Etat)

//Prototypes
void automate(int tddv_estime, int tab_para_automate[][3], enum Etat *etat);
void fct_test(int scenario[]);

Then you have to instantiate an enum object before calling automate:

enum Etat etat;
automate(tddv_estime, tab_para_automate, &etat);

This is how you use enums correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the problem was that i didn't instanciate an enum object before calling my function. I have another question, once an enum object is instanciated, can i use it in another function calling ?
Yes, you can treat an enum object just like every other object (i.e. a struct), there is not much difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.