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
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 instdio.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.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:There are multiple problems:
remove
should be renamed to prevent a conflict with the standard functionremove
declared asint remove(const char *filename);
in<stdio.h>
that deletes a file from the file system (using theunlink
system call on POSIX systems).main()
has been obsolete for quite some time. The proper prototype formain
without arguments isint main(void)
.You should also remove the
const
qualifier on the return type of theremove
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: