just writing and testing the code using arrays,

int main()
{   int i,n;


    int arr[14];

    printf("\nEnter the array ",n);
    scanf("%d",&n);
    int lastindex =  n-1;
    int *l;
    l = &lastindex;
    for(i = 0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    for(i = 0;i<n;i++)
    printf("\n%d\n",arr[i]);
    printf("%d",*l);
    //arr[i] = lastindex + 2;
    //printf("\n%d",arr[i]);
    if(arr[lastindex[arr]]  < lastindex)
    {
        printf("\n %d",arr[lastindex[arr]]);
        printf("\nright");
    }
    else
    {   printf("\n%d",lastindex);
        printf("wrong");

    }
}

//output
Enter the array 5
5
4
3
2
1

5

4

3

2

1
4

4 wrong
arr[lastindex[arr]]
lastindex[arr] will be 4
arr[4]= 1
1 < 4 is true so it has to print right,but its printing wrong
whats the wrong in the code

Recommended Answers

All 13 Replies

arr[4]= 1
1 < 4 is true so it has to print right,but its printing wrong

Yes, but that isn't what is being compared. arr[1] is 4

couldn't get you,can you explain it please

lastindex[arr] is the same thing as arr[lastindex], which is 1, not 4. So the comparsion can be rewritten like this:
if(arr[arr[lastindex]] < 4)

Now, start from the innermost expression arr[lastindex] which is 1, then evaluate arr[1] which is 4.

got it,Thanks

BTW, don't use index[arr] because that form is rarely, if ever, used in real life. If you intend to index an array, then do so like this: arr[index]

but to how to write the code to return the lastindex first

you mean you want to display the data in reverse order, such as 1 2 3 4 5 instead of 5 4 3 2 1? You already have a pointer named l, just use it

int* l = &arr[lastindex];
while( l > arr )
{
   printf("%d\n", *l);
   --l;
 }

or you could do this:

while( lastindex >= 0)
{
   printf("%d\n", arr[lastindex]);
   --lastindex;
}

i need to return the lastindex of the array and then element in the index,how to do that

no,not in reverse order,say n is 5 and the last array index is 4,and then have to print the element in the last index

then just print arr[lastindex] (assuming the value of lastindex == 4)

how to write in code ,to return the last index and the element in the lastindex

Dude, go read a book on C. You're asking for something so trivial that I can't imagine anything other than total ignorance as the cause. This forum is not for holding your hand and teaching you from scratch.

how to write in code ,to return the last index and the element in the lastindex

lastindex is not an array, so your question makes no sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.