【问题标题】:C collections and pointers error - assignment from incompatible pointer typeC 集合和指针错误 - 来自不兼容的指针类型的赋值
【发布时间】:2020-07-05 09:22:39
【问题描述】:

我正在学习 C。我尝试解决以下问题,但遇到了几个问题。

我不用指针控制链表。

问题出在这里:

我们想编写用于管理公司员工的函数。员工定义为 他的名字(字符串),他的人事编号(整数),工作小时数(实际),和小时费率(实际)。

  1. 定义员工数据类型
  2. 功能:
  • saisir雇员;它允许您从键盘输入员工
  • affich雇员;在屏幕上显示员工
  • 计算工资;计算并返回员工的工资(salary = nbh * rate_h)
  1. 我们现在想将一组员工存储在一个链表中,为此定义节点和 ListEmp 数据类型。
  • 外籍雇员;这允许您将新员工添加到列表中(添加可以是 在列表的开头或结尾执行,您的选择)
  • saisirList 雇员;它允许您从键盘输入 n 名员工的列表。
  • affichListemploye;在屏幕上显示员工列表。
  • 总工资;计算并返回列表中所有员工的总工资
  1. 编写一个执行以下操作的主函数:
  • 声明员工列表
  • 使用键盘输入员工人数
  • 进入列表
  • 计算所有员工的总工资
  • 显示列表内容,以及工资总额

我的代码:

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

typedef struct
{
 char nom[20];
 int mat;
 float ht;
 float tx ;
}Employe;


typedef struct
{
    Employe *employe;
    struct ListEmp *suivant;
}ListEmp;

typedef struct 
{
    ListEmp *premier;
}Elements;


void saisirEmploye(Employe *e)
{
    printf("Sasir le nom \n");
    scanf("%s",e->nom);
    printf("Saisir le matricule \n");
    scanf("%d",&e->mat);
    printf("Saisir le nombre d’heures travaillees \n");
    scanf("%f",&e->ht);
    printf("Saisir le taux horaire \n");
    scanf("%f",&e->tx);

}

void afficheEmp(Employe e)
{
    printf("Nom : %s | Matricule : %d | Nombre d’heures travaillees : %f | Taux horaire : %f \n",e.nom,e.mat,e.ht,e.tx);
}

float calculSalaire(Employe e)
{
    float salaire = 0;
    salaire = (e.ht)*(e.tx) ;
    return salaire;
}

Elements *init()
{
    ListEmp *liste = malloc(sizeof(ListEmp));
    Elements *elements = malloc(sizeof(Elements));

    if (liste == NULL || elements == NULL)
    {
        exit(EXIT_FAILURE);
    }

    liste->employe = NULL;
    liste->suivant = NULL;
    elements->premier = liste;

    return elements;
}


void ajouterEmp(Elements *elements, Employe *employe)
{
    /* Création du nouvel élément */
    ListEmp *nouveau = malloc(sizeof(*nouveau));
    
    if (elements == NULL || nouveau == NULL)
    {
        exit(EXIT_FAILURE);
    }
    
    nouveau->employe = employe;

    /* Insertion de l'élément au début de la liste */
    nouveau->suivant = elements->premier;
    elements->premier = nouveau;
}


int main()
{
 // Employes e;
 // e.tete=NULL;
 // int n=0;
 // float t=0;
 // printf("Donner le nombre des Emplyes \n"); // saisie du nbr des employes
 // scanf("%d ",&n);
 // saisirListEmp(&e,n); // saisie de la liste
 // t=totalSalaire(e); //calcule du salaire totale
 // afficheList(e); // affichage du contenu
 // printf("le salaire totale=%f ",t); // affichage du salaire
 
 
 Employe e;
 printf("Saisr un emplye \n");
 saisirEmploye(&e);
 //printf("Nom emplye : %s \n", e.nom);
 afficheEmp(e);
 printf("Salaire : %f",calculSalaire(e));
 
 Elements *maListe = init();
 ajouterEmp(maListe,e)
 
 return 0;
}

修改后的代码:

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

typedef struct
{
    char nom[20];
    int mat;
    float ht;
    float tx;
} Employe;


typedef struct Elem
{
    Employe employe;
    struct Elem *prev;
    struct Elem *next;
}Elem;

typedef struct
{
    Elem *first;
    Elem *last;
}ListEmp;


void saisirEmploye(Employe *e)
{
    printf("Sasir le nom \n");
    scanf("%s",e->nom);
    printf("Saisir le matricule \n");
    scanf("%d",&e->mat);
    printf("Saisir le nombre d’heures travaillees \n");
    scanf("%f",&e->ht);
    printf("Saisir le taux horaire \n");
    scanf("%f",&e->tx);

}

void afficheEmp(Employe e)
{
    printf("Nom : %s | Matricule : %d | Nombre d’heures travaillees : %f | Taux horaire : %f \n",e.nom,e.mat,e.ht,e.tx);
    printf("\n");
}

float calculSalaire(Employe e)
{
    float salaire = 0;
    salaire = (e.ht)*(e.tx) ;
    return salaire;
}


ListEmp *init()
{
    ListEmp *listEmp = malloc(sizeof(ListEmp));
    Elem *elem = malloc(sizeof(Elem));
    Employe employe;

    if (listEmp == NULL || elem == NULL)
    {
        exit(EXIT_FAILURE);
    }
    strcpy(employe.nom, "Hamza");
    employe.mat = 123;
    elem->employe = employe;
    elem->next = NULL;
    listEmp->first = elem;

    return listEmp;
}

void auDebut(ListEmp *listEmp, Employe employe)
{
    printf("1 Au debut \n");
    Elem *elem = (Elem*)malloc(sizeof(Elem));
    elem->employe = employe;
    elem->next = listEmp->last;
    elem->prev = NULL;
    if(listEmp->last)
        listEmp->last->prev = elem;
    else
        listEmp->last = elem;
    listEmp->first = elem;
}

void aLaFin(ListEmp *listEmp, Employe employe)
{
    
    Elem *elem = (Elem*)malloc(sizeof(Elem));
    elem->employe = employe;
    elem->prev = listEmp->first;
    elem->next = NULL;
    if(listEmp->first)
        listEmp->first->next = elem;
    else
        listEmp->first = elem;
    listEmp->last = elem;
    
//    Elem *elem = (Elem*)malloc(sizeof(Elem));
//    if(!elem) exit(EXIT_FAILURE);
//    elem->employe = employe;
//    elem->next = listEmp->first;
//    listEmp->first = elem;
}





void ajouterEmploye(ListEmp *listEmp, Employe employe)
{
    char element;
    printf("Voulez-vous ajouter le nouvel employe au début de la liste d ou a la fin de la liste f ? \n");
    scanf(" %c", &element);
    if (element == 'd')
        //printf("Au debut \n");
        auDebut(listEmp,employe);
    else if (element == 'f')
        //printf("A la fin \n");
        aLaFin(listEmp,employe);
}

void saisirListEmploye(ListEmp *listEmp, int n)
{
    Employe employe;
    for(int i=1;i<=n;i++)
    {
        printf("Employe %d\n",i);
        saisirEmploye(&employe);
        ajouterEmploye(listEmp,employe);
    }
}

void affichListEmploye(ListEmp *listEmp)
{
    Elem *i = listEmp->first;
    while(i != NULL)
    {
        afficheEmp(i->employe);
        i = i->next;
    }
}


float totalSalaire(ListEmp *listEmp)
{
    float total = 0;
    Elem *i = listEmp->first;
    while (i != NULL)
    {

        total += calculSalaire(i->employe);
        i = i->next;
    }
    return total;
}

int main()
{
 
    Employe e;
    int n;
    //printf("Saisr un emplye \n");
    //saisirEmploye(&e);
    //printf("Nom emplye : %s \n", e.nom);
    //afficheEmp(e);
    //printf("Salaire : %f",calculSalaire(e));
    //printf("\n\n");
    
    ListEmp *listEmp = init();
    //ajouterEmploye(listEmp,e);
    
    printf("Combien d'employes souhaitez-vous ajouter ?\n");
    scanf("%d",&n);
    saisirListEmploye(listEmp,n);
    
    affichListEmploye(listEmp);
    printf("Le salaire total de tous les employés de la liste est : %f", totalSalaire(listEmp));

    return 0;
}

错误:

main.c: In function ‘ajouterEmp’:
main.c:87:22: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     nouveau->suivant = elements->premier;
                      ^
main.c: In function ‘main’:
main.c:114:21: error: incompatible type for argument 2 of ‘ajouterEmp’
  ajouterEmp(maListe,e)
                     ^
main.c:76:6: note: expected ‘Employe * {aka struct  *}’ but argument is of type ‘Employe {aka struct }’
 void ajouterEmp(Elements *elements, Employe *employe)
      ^~~~~~~~~~
main.c:116:2: error: expected ‘;’ before ‘return’
  return 0;
  ^~~~~~

你能帮帮我吗? 谢谢!

【问题讨论】:

    标签: c pointers collocation


    【解决方案1】:

    关于你的类型错误:有定义

    typedef struct {
        char  nom[20];
        int   mat;
        float ht;
        float tx;
    } Employe;
    

    但后来你使用Employes而不是Employe,例如在

    void ajouterEm (Employes *e, Elem nemp) { /* ... */ }
    

    这里的ElementsElem 相同。这就是您收到未知类型错误的原因。在

    ajouterEmp(maListe, e)
    

    指令后还必须有一个分号。还应注意将指针(例如Employes*)和整个结构(没有*)传递给函数的位置。

    修复此错误后编辑:

    typedef struct{
        Employe *employe;
        struct ListEmp *suivant;
    } ListEmp;
    

    类型名称ListEmp 已经在suivant 的定义中使用,但仅在下面的行中定义。使用

    typedef struct _ListEmp {
        Employe *employe;
        struct _ListEmp *suivant;
    } ListEmp;
    

    相反。该结构获得名称_ListEmp,然后将struct _ListEmp 定义为ListEmp。对于第三个错误

    ajouterEmp(maListe, e)
    

    必须传递一个指针,但Employe e是一个完整的数据结构。写

    ajouterEmp(maListe, &e);
    

    改为添加分号。

    【讨论】:

    • 谢谢,我编辑了代码。我的问题是这个函数:ajouterEmp.
    • 我编辑了我的代码。它工作得更好,但是,我认为我有最后一个指针问题。请检查一下好吗?
    • 奇怪,对我来说(我使用 gcc 5.4.0)现在可以毫无问题地编译代码并且似乎可以工作(除了硬编码的测试用例和我生疏的法语知识)。你能更新一下错误信息吗?
    猜你喜欢
    • 2019-08-09
    • 2017-10-24
    • 2014-12-14
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多