skip to Main Content

I am translating a program from Pascal to C. It has been many years since I used the C language. Here is a relevant code snippet:

typedef struct querec *queptr;
struct querec {
    char item;
    queptr next;
};
struct queue {
    queptr front,
    rear;
};
struct queue qnum[5];
char item;

const char remove(struct queue *q)
{
    queptr p;
    char remove_result;

    p = q->front;
    remove_result = p->item;
    q->front = p->next;
    if (q->front == NULL)
        q->rear = NULL;
    return remove_result;
}

main()
{
    ...
    item = remove(&qnum[3]);
}

It will not compile in Visual Studio 2015, reporting the following:

Warning C4133   'function': incompatible types - from 'queue *' to 'const char *'
Error   C2373   'remove': redefinition; different type modifiers

3

Answers


  1. The C language does not have function overloading, and it happens that int remove(const char*) is a standard C library function.

    To fix the error, you need to call your function something else, such as queue_remove for example.

    Note that when the compiler gives errors like this, it will usually output additional information showing you the conflicting definitions. In this case, it should have shown you that remove was declared in stdio.h or a related internal file. Even though it can feel like information overload, it’s always worth taking time to read the message carefully.

    Login or Signup to reply.
  2. I’d be careful about using the name remove.

    It is a standard function, defined in stdio.h.

    See: man 3 remove It has a prototype of:

    int remove(const char *pathname);
    
    Login or Signup to reply.
  3. There are multiple problems:

    • remove should be renamed to prevent a conflict with the standard function remove declared as int remove(const char *filename); in <stdio.h> that deletes a file from the file system (using the unlink system call on POSIX systems).

    • main() has been obsolete for quite some time. The proper prototype for main without arguments is int main(void).

    • You should also remove the const qualifier on the return type of the remove function.

    • hiding pointers behind typedefs is confusing and error prone, especially when the type name is unrelated to the pointed to type.

    Here is a modified version:

    #include <stdio.h>
    
    typedef struct queue_record queue_record;
    typedef struct queue queue;
    
    struct queue_record {
        char item;
        queue_record *next;
    };
    
    struct queue {
        queue_record *front;
        queue_record *rear;
    };
    
    char queue_remove(queue *q)
    {
        queue_record *p;
        char remove_result = 0;
    
        p = q->front;
        if (p) {
            remove_result = p->item;
            q->front = p->next;
            if (q->front == NULL)
                q->rear = NULL;
            // XXX: should probably free(p)?
        }
        return remove_result;
    }
    
    int main(void)
    {
        queue qnum[5];
        char item;
    
        //...
        item = queue_remove(&qnum[3]);
        //...
        return 0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search