Reputation: 5416
So, I have written a function for insertion into a queue viz en_queue
. The function body is:
void en_queue(queue,max,front,rear)
{
int ch1;
printf("\n Enter element to add->");
scanf("%d",&ch1);
if(front==0 && rear==(max-1))
{
printf("\n Caution!! Queue Overflow!!");
}
else if(rear==(max-1) && front>0)
{
rear=0;
*queue[rear]=ch1;
}
else if(front==-1 && rear==-1)
{
front=rear=0;
*queue[rear]=0;
}
else
{
rear++;
*queue[rear]=ch1;
}
}
And the prototype declaration is:
void en_queue(int *,int, int, int);
And during calling, I called it like:
en_queue(queue,MAX,front,rear);
where queue[MAX]
is an array (#define MAX 10
), MAX
is the number of elements the array can incorporate and both front
and rear
are integers with current value -1
. I am getting repeated errors of type error: pointer expected
and Conflicting argument declarations for function 'en_queue'.
I can't see why arguments are not matching. Because I passed an address of the array and received it in a pointer variable and worked with the pointer. So how can there be error?
Upvotes: 2
Views: 3951
Reputation: 58271
First:
void en_queue(queue,max,front,rear)
{
should be:
void en_queue(int *queue, int max, int front, int rear)
{
And second, expressions like *queue[rear] = 0;
should be just queue[rear] = 0;
because queue
is int*
.
Note: An expression a[i]
== *(a + i)
, so if a
is pointer then in expression a[i]
you doesn't need to deference *
explicitly.
In your expression *queue[rear]
, you are getting error type error: pointer expected
because you dereference twice e.g. *queue[rear]
== *
*(queue + rear)
because *(queue + rear)
is not pointer but a int hence compiler message is need a pointer.
Compiler message: "Conflicting argument declarations for function en_queue()
" because of first argument is int*
. As in function definition you doesn't specify type of arguments default is considered int type.
Upvotes: 3
Reputation: 4314
While you can write a declaration without naming your arguments, just giving the type specifiers, you cannot write a definition with only the names and without the specifiers. The conflicting argument declarations
error is because of this, because the compiler interprets the argument names in your definition as types rather than names. Think about it, how could the compiler know the difference between a name given without a type and a type given without a name?
As @grijesh has written, you should write your definition as:
void en_queue(int *queue, int max, int front, int rear)
instead.
The other problem is that you're dereferencing your pointer twice, which doesn't make sense with a pointer to an integer. An array and a pointer are similar in the way that they give a memory address that you can use to do further indexing. This can be done by using the regular index operator:
queue[index]
or using the explicit pointer dereferencing operator *
, and pointer arithmetic like this:
*(queue + index)
Read more about pointer arithmetic here.
Upvotes: 2
Reputation: 61910
If queue
is int *
then the correct way to access it as an "array" is
queue[index];
and NOT *queue[index]
queue[index]
is actually *(queue + index)
.
if you do *queue[index]
you are actually doing *(*(queue + index))
.
(queue + index)
will get to the address location index
locations away from the addres stored in queue
. *(queue + index)
will dereference it by getting the value stored at the address. *(*(queue + index))
will use the fetched value as an address and try to make another fetch, which is wrong because now you dereference an integer.
Upvotes: 3
Reputation: 6116
You said: ..where queue[MAX] is an array..
Your declaration is: void en_queue(int *,int, int, int);
Then what does this mean: *queue[rear]=ch1;
You are trying to dereference an integer
Correct all the instances in your program like above to: queue[rear] = ch1;
Upvotes: 0