在 C 中使用结构重载 [英] Overloading with struct in C

查看:57
本文介绍了在 C 中使用结构重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个具有相同名称的函数,但它接受 3 种不同类型的结构.我不知道如何编写参数来执行此操作.所以在 _______ 中,应该有 proffesore、amminustratore,学生.Sp 表示该函数可以接受所有 3 种类型的结构,但一次只能接受一种.代码如下:

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;
}

推荐答案

我建议像这样的单个 struct 类型

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;

那么你就有了三个链表

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.

这篇关于在 C 中使用结构重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆