I am implementing a dequeue in c via arrays. left and right are pointers which point to the leftmost and rightmost elements of the dequeue. The show() function recieves the left and right pointers. When i try the following in void show(int *l,int *r), the function produces wrong output-
int *t;
for(t=l;t<r;t++);
{
printf("%d-->",*t);
}
printf("%d\n",*t);
But when i try this it works-
for(t=l,i=0;i<r-l;i++,t++)
printf("%d-->",(*t));
printf("%d\n",*r);
Obviously comparison between pointers in the first code is not working, even though they point to members of the same array, Why is this hapening?
Edit- Here is the whole function
void show(int *l,int *r)
{
if(l==r && r==NULL)
{
printf("underflow\n");
}
else
{
int *t,i;
for(t=l;t!=r;t++);
{
printf("%d-->",*t);
}
printf("%d\n",*r);
/* for(t=l,i=0;i<r-l;i++,t++)
printf("%d-->",(*t));
printf("%d\n",*r);*/
}
}
The commented out region is not working in show().
Question closed, silly error!!!
See the semicolon there? Remove it. As is, the loop increments
tuntilris reached without doing anything, then the value pointed to bet(nowr) is printed, followed by “–>”, and then the value pointed to byr.