Overloading with struct in C

匆匆过客 提交于 2021-01-28 12:21:44

问题


I am trying to write a function with the same name, but that accepts 3 different typers of structs.I don't know how I can write the parameters to do this.So in the _______, there should be proffesore, amminustratore, studente. Sp that the function can accept all 3 types of struct but only one at the time. Here is the code:

int confermaCredenziali(struct ______ dati, char *uN, char *pW);

struct amministratore{
  char userName[MAX_LNG];
  char passWord[MAX_LNG];
  int stato;
  struct ammin *next;
};
struct professore{
  int ID;
  char userName[MAX_LNG];
  char passWord[MAX_LNG];
  int stato;
  struct prof *next;
};

struct studente{
  int ID;
  char *userName[MAX_LNG];
  char *passWord[MAX_LNG];
  int punti;
  int stato;
  struct studente *next;
};


int confermaCredenziali(struct ______ dati, char *uN, char *pW){
 while (dati != NULL) {
  if (strcmp(dati->userName, uN) == 0 && strcmp(dati->passWord, pW) == 0){
   if (dati->stato == 1)
    return 1;
   else{
    printf("Il suo stato e' passivo.\n");
    return 0;
   }
 }
 dati = dati->next;
 }
 printf("Credeziali errate.\n");
 return 0;
}

回答1:


I suggest a single struct type like this

typedef struct person {
      char userName[MAX_LNG];
      char passWord[MAX_LNG];
      int stato;
      struct person *next;
} person_t;

Then you have three linked lists

person_t *amministratore = NULL;
person_t *professore     = NULL;
person_t *studente       = NULL;

Also, the function needs a pointer argument, I suggest

int confermaCredenziali(person_t *dati, char *uN, char *pW)

Then you pass the head of the linked list you have selected, to the function. It works with one list at a time, and the three lists are completely separate.




回答2:


As stated earlier in comments:

Create as follows:

  • Include all three struct as members of a union.
  • add the union as a member of a collector struct.
  • Add an additional enum member to the collector struct to identify which of the three struct you are passing.

The following illustrates:

typdef enum {
    ADMIN,
    PROF,
    STUD,
    MAX_S
}S_TYPE;  

typedef struct {
    ...
}amministratore;

typedef struct {
    ...
}professore;

typedef struct {
    ...
}studente;

typedef union{
    administratore admin;
    professore prof;
    studente stud;
}s_union;

typedef struct {
    s_union;
    S_TYPE type;
}collector;

Your prototype would be modified to:

int confermaCredenziali(collector *c, S_Type type, dati, char *uN, char *pW);

As mentioned also in comments, the argument for this struct as passed in the prototype should be as pointer, so that the members can be changed, and for efficiency, as a pointer is often times smaller than the entire struct




回答3:


this example is trivial but you can do something like this:

enum type{STUD = 0, PROF, ADMIN};

int confermaCredenzialiSTUD(void *dati, char *uN, char *pW);
int confermaCredenzialiPROF(void *dati, char *uN, char *pW);
int confermaCredenzialiADMIN(void *dati, char *uN, char *pW);

const int (*credenziali[3])(void *, char *, char *) = 
        {confermaCredenzialiSTUD, confermaCredenzialiPROF, confermaCredenzialiADMIN};

int confermaCredenziali(enum type t, void *dati, char *uN, char *pW)
{
    return redenziali[t](dati, uN, pW);
}

and in the functions do correctly what you need for the particular type.



来源:https://stackoverflow.com/questions/62939233/overloading-with-struct-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!