Search code examples
cpointersdata-structuresdeque

comparison between pointers


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!!!


Solution

  • for(t=l;t!=r;t++);
    

    See the semicolon there? Remove it. As is, the loop increments t until r is reached without doing anything, then the value pointed to be t (now r) is printed, followed by "-->", and then the value pointed to by r.