smi2334
smi2334

Reputation: 21

rename() function differs in levels of indirection from int

I'm trying to write a code that uses linked lists and files, to maintain actions (functions) that are given on a text file, instead of receiving them from the user.

For some reason i'm encountering an error which says:

"Severity Code Description Project File Line Suppression State Error C2040 'rename': 'hw_component *(char *,char *,hw_component *)' differs in levels of indirection from 'int (const char *,const char *)' EX5_313410961 c:\users\edave\source\repos\ex5_313410961\ex5_313410961\shahar_connection.c 172"

I'm having trouble understanding if the problem is the actual function rename() or because of the actions() function.

The code is very long so ill post the relevant section only.

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

Upvotes: 0

Views: 91

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

You library already defines a function called rename(), which is prototyped in and included by adding stdio.h, which is creating the conflict with your user-defined function.

As per the lined manual, the signature of the library-defined rename() is int rename (const char *, const char*), whereas as er your function definition, you have a signature as hw_component *rename(char *, char *, hw_component *) - which does not match and your compiler is showing you correct warning (and error) message.

Solution: Use a different name for your function.

Upvotes: 2

Lundin
Lundin

Reputation: 213458

You cannot name your custom function rename while including stdio.h, because the name collides with the standard library function rename. Name your own function something different.

The standard function is

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

So it has type int (const char *, const char *), the same type that the compiler is complaining about a conflict with.

Upvotes: 2

Related Questions