【问题标题】:rename() function differs in levels of indirection from intrename() 函数与 int 的间接级别不同
【发布时间】:2020-06-09 11:03:15
【问题描述】:

我正在尝试编写一个使用链表和文件的代码,以维护文本文件中给出的操作(函数),而不是从用户那里接收它们。

由于某种原因,我遇到了一个错误,上面写着:

"严重性代码描述项目文件行抑制状态 错误 C2040 'rename': 'hw_component *(char *,char *,hw_component *)' 与 'int (const char *,const char *)' 的间接级别不同 EX5_313410961 c:\users \edave\source\repos\ex5_313410961\ex5_313410961\shahar_connection.c 172"

我无法理解问题是实际函数rename() 还是因为actions() 函数。

代码很长,所以只发布相关部分。

#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define NAME_LENGTH 200

typedef struct hw_component
{
    char name[NAME_LENGTH];
    int copies;
    struct hw_component *next;
}hw_component;

hw_component *create_component(char* name,int copies)
{
    if (name != NULL && strlen(name) > NAME_LENGTH)
        return NULL;
    hw_component *comp = (hw_component*)malloc(sizeof(hw_component));
    if (comp == NULL)
    {
        printf("Error: memory allocation failed\n");
        return NULL;
    }
    strcpy(comp->name, name);
    comp->copies = copies;
    comp->next = NULL;
    return comp;
}

hw_component *add_sort(hw_component* head, int copies, char *name)
{
    hw_component *iter, *prev = NULL;
    hw_component *new_comp = create_component(name, copies);
    if (head == NULL)
        return new_comp;
    if (strcmp(new_comp->name, head->name) < 0)
    {
        new_comp->next = head;
        return new_comp;
    }
    iter = head;
    while (iter != NULL && strcmp(iter->name, new_comp->name) < 0)
    {
        prev = iter;
        iter = iter->next;
    }
    prev->next = new_comp;
    new_comp->next = iter;
    return head;
}

hw_component *initialize(char *argv[])
{
    hw_component *list_of_comp = NULL;
    FILE *fp = NULL;
    char dolar[2] = "$";
    fp = fopen(argv[1],"r");
    if (fp == NULL)
    {
        printf("Error: opening %s failed\n", argv[1]);
        exit(1);
    }
    while (!feof(fp))
    {
        char str[400], *token, *str_num;
        fgets(str, 400, fp);
        int numb;
        token = strtok(str,dolar);
        str_num = strtok(NULL, dolar);
        numb = atoi(str_num);
        list_of_comp=add_sort(list_of_comp, numb, token);
    }
    fclose(fp);
    return list_of_comp;
}

void finalize(hw_component *head, char *argv[])
{
    hw_component *temp;
    FILE *fp = NULL;
    int temp_num;
    char *str,*num_str;
    fp = fopen(argv[3], "w");
    if (fp==NULL)
    {
        printf("Error: opening %s failed\n", argv[3]);
        while (head!=NULL)
        {
            temp = head;
            head = head->next;
            free(temp);
        }
    }
    while (head!=NULL)
    {
        temp = head;
        fprintf(fp,"%s $$$ %d\n",temp->name,temp->copies);
        head = temp->next;
        free(temp);
    }
    fclose(fp);
}

hw_component *remove_comp_by_name(hw_component *head_comp, char comp_name[NAME_LENGTH + 1])
{
    hw_component *temp_ptr = head_comp;
    hw_component *prev_ptr;
    hw_component *zero_copies_check = head_comp;


    if (head_comp == NULL)
    {
        return head_comp;
    }

    if (strcmp(temp_ptr->name, comp_name) == 0)
    {
        head_comp = head_comp->next;
        free(temp_ptr);
        return head_comp;
    }

    while (temp_ptr != NULL && strcmp(temp_ptr->name, comp_name) != 0)
    {
        prev_ptr = temp_ptr;
        temp_ptr = temp_ptr->next;
    }

    if (temp_ptr != NULL)
    {
        prev_ptr->next = temp_ptr->next;
        free(temp_ptr);
    }

    while (zero_copies_check != NULL)
    {
        if (zero_copies_check->copies == 0)
        {
            free(zero_copies_check);
        }

        zero_copies_check = zero_copies_check->next;
    }

    return head_comp;

}

hw_component *rename(char old_name[NAME_LENGTH], char new_name[NAME_LENGTH], hw_component *head_comp)
{
    hw_component *temp_comp = head_comp;
    int num_of_copies = 0;

    if (head_comp == NULL)
    {
        return head_comp;
    }

    while (temp_comp != NULL && strcmp(temp_comp->name, old_name) != 0)
    {
        temp_comp = temp_comp->next;
    }

    num_of_copies = temp_comp->copies;

    head_comp=remove_comp_by_name(head_comp, old_name);
    head_comp=add_sort(head_comp, num_of_copies, new_name);
    return head_comp;
}

hw_component *return_comp(hw_component *head, char name_of_comp[], int copies)
{
    hw_component *temp_comp = head;
    if (head == NULL)
    {
        return head;
    }

    while (temp_comp != NULL && strcmp(temp_comp->name, name_of_comp) != 0)
    {
        temp_comp = temp_comp->next;
    }
    if (temp_comp != NULL)
    {
        temp_comp->copies += copies;
    }
    else
    {
        add_sort(head, name_of_comp, copies);
    }
    return head;
}

int choose_act(char *str)
{
    char init[12] = "Initialize ", rename[8]="Rename ",fire[6]="Fire ";
    char retu[24] = "Returned_from_customer ", prod[12]="Production " ,fatal[19]="Fatal_malfunction ";
    char finalize[] = "Finalize";
    if (strcmp(str,init)==0)
        return 1;
    if (strcmp(str, rename)==0)
        return 2;
    if (strcmp(str, retu)==0 || strcmp(str, prod)==0)
        return 3;
    if (strcmp(str, fire)==0 || strcmp(str, fatal)==0)
        return 4;
    if (strcmp(str, finalize)==0)
        return 5;
}
void actions(char *argv[])
{
    char dolar[2] = "$";
    hw_component *head;
    FILE *fp = NULL;
    char str[400], *token, *name, *old_name;
    int choise = 0, numb;
    fp = fopen(argv[2], "r");
    if (fp == NULL)
    {
        printf("Error: opening %s failed\n", argv[2]);
        exit(1);
    }
    while (!feof(fp))
    {
        fgets(str, 400, fp);
        token = strtok(str,dolar);
        choise = choose_act(token);
        head = initialize(argv);
        switch (choise)
        {
        case 1:
            break;
        case 2:
            old_name = strtok(str, dolar);
            name = strtok(str, dolar);
            printf("%s %s", old_name, name);
            head=rename(old_name,name,head);
            break;
        case 3:
            name = strtok(str, dolar);
            numb = atoi(strtok(str, dolar));
            head=return_comp(head, name, numb);
            break;
        case 4:
            name = strtok(str, dolar);
            numb = atoi(strtok(str, dolar));
            head=fatal_maf(head, name, numb);
            break;
        case 5:
            finalize(head,argv);
            break;
        default:
            break;
        }
    }
}
int check_argc(int argc)
{
    int numb = argc;
    if (argc != 3)
    {
        printf("Error: invalid number of arguments (<%d> instead of 3)\n", numb);
        return 0;
    }
    return 1;
 }

int main(int argc, char *argv[])
{
    int res = 0;
    res=check_argc(argc);
    if (res == 1)
        actions(argv);
    return 0;
}

【问题讨论】:

    标签: c


    【解决方案1】:

    您的库已经定义了一个名为 rename() 的函数,它通过添加 stdio.h 进行原型化和包含,这会与您的用户定义函数产生冲突。

    根据内衬手册,库定义的rename() 的签名是int rename (const char *, const char*),而作为您的函数定义,您的签名为hw_component *rename(char *, char *, hw_component *) - 这不匹配并且您的编译器正在向您显示正确的警告(和错误)消息。

    解决方案:为您的函数使用不同的名称。

    【讨论】:

      【解决方案2】:

      您不能在包含stdio.h 的同时将自定义函数命名为rename,因为该名称与标准库函数rename 冲突。将您自己的函数命名为不同的名称。

      标准函数是

      int rename(const char *old, const char *new);
      

      所以它的类型为int (const char *, const char *),与编译器抱怨与之冲突的类型相同。

      【讨论】:

        猜你喜欢
        • 2019-06-13
        • 1970-01-01
        • 2020-04-07
        • 2013-02-18
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 2019-06-21
        • 2020-01-05
        相关资源
        最近更新 更多