Check input of enum

0

I should make a function that controls the insertion of an enum, if the insertion is correct it must do nothing, if it is wrong it must allow the user to re-insert it.

these are the portions of my code:

typedef enum {SHORT= 1, MEDIUM, LONG, OTHER} hair;

typedef struct{

...

hair type;

}record;

record subj[DIM_RECORD];

printf("\ntype= ");

printf("\n\t%d) SHORT",SHORT); //Output= 1)Short etc

printf("\n\t%d) MEDIUM",MEDIUM);

printf("\n\t%d) ILONG",LONG);

printf("\n\t%d) OTHER",OTHER);

isEnum(subj->type);

void isEnum(int* i){ 
  do { 
      scanf("%d", &i); 
      if (*i>=1 && *i <= 4) break; 
      printf("\nError"); 
      printf("\ntry again "); 
   }while(1); 
}

I don't understand where I'm wrong, any number I enter always gives me a error (Process finished with exit code -1073741819 (0xC0000005))

c
asked on Stack Overflow Jan 3, 2021 by Zeld • edited Jan 3, 2021 by Zeld

2 Answers

0
void isEnum(int* i){ 
  do { 
      scanf("%d", &i); 

You are passing in a pointer-to-int as i

You are passing a pointer-to-pointer-to-int to scanf.

If you change scanf to be scanf("%d",i) then this function should work.

Also,

isEnum(subj->type);

This is passing 'type' as an enum (int), not as a pointer-to-int. Try:

isEnum(&subj[0]->type);

When you reference a member of a structure, you are doing so by value - if you want to modify it, you need to pass the pointer to it.

answered on Stack Overflow Jan 3, 2021 by Halt State • edited Jan 3, 2021 by Halt State
0

Given this declaration ...

record subj[DIM_RECORD];

..., where record is a structure type, this call, though technically valid, is misleading:

isEnum(subj->type);

As a matter of clarity and style, I urge you to write it in this equivalent form, instead:

isEnum(subj[0].type);

Among other things, if you did that then you would

  1. be better positioned for initializing records other than the first, and
  2. be more likely to recognize the type mismatch in your call.

Your compiler really ought to be warning you about the type mismatch already, however. Do not ignore compiler warnings, especially when your program is not behaving as you think it should.

Having corrected the call to

isEnum(&subj[0].type);

, you have another issue. In isEnum(), i is already a pointer to the object you want to initialize. Therefore, this ...

      scanf("%d", &i); 

... is incorrect. You want to store the data read in the location to which i points, not to replace the pointer value of i itself. Moreover, the type of &i, int **, is not correctly type-matched to the %d format directive, so that call produces undefined behavior.

Even if you suppose that scanf's undefined behavior in this case is localized to trashing the value of i, that leads to further misbehavior when you attempt to dereference that trashed value with *i. That is probably where the program actually crashes, as it is trying to dereference a pointer to who knows where. Correct that chain of errors by fixing the scanf call:

      scanf("%d", i); 
answered on Stack Overflow Jan 3, 2021 by John Bollinger

User contributions licensed under CC BY-SA 3.0