Hi, Im new to C programming and have some confusion of using pointer on arrays with this code. void main() { int arr[] = {10,20,30,40,50}; int *ptr = arr; printf(" %d %d ",*ptr++,*ptr); } the output is 10,10 but I think it should be 11,10. would appreciate if someone explains the logic of the output.
the value in the array which the pointer is pointing to is being incremented by 1 by the ++ operator,so the first element is 10 in the array which the pointer is pointing to and increment by 1-->11.
Ok, the printf (*ptr++,*ptr) now if *ptr points to 10,which is the first element in the array, then *ptr++ should be 11. I assumed 11 because of the (++) increment operator.*ptr remains 10. thats where my confusion lies I think? since ++ is a postfix operator ,the value of 10 is assigned first, thats the only way I can think of 10,10 output.
You are viewing 10, 10 because of the parameters is passed as right to left by your compiler to the function and so after passing 10 (the right most) and then again the 10 it increments. There are left to right passing as well and so the statement is not very standard and is only used by some beginners and nothing more.
I agree with shabbir.... but one thing i want to add is *ptr++ will never increment the value pointed by the pointer ptr. It will return the value and then the pointer will be incremented or advanced to the next value in the array... if someone want to increment the value pointed to by the pointer you should use ++*ptr or (*ptr)++...
hi Actually here is the issue of precedence i.e. ++(post increment operator) and *(value at address operator) both have equal priority but right to left associativity. So ++ is found at most right position and than *. expression is something like *(ptr++). now ptr is holding the base address of arr[](i.e. address of arr[0]) suppose it is 4020 after ++ it will hold the address of next element arr[1], which will 4022.But it is post increment so first assignment then increment so right now value is 4020. now expression will be *(4020), value at address 4020 that is 10.... Hope you can understand now.
Hi Neha, If you go with the precedence, then postfix(++) have higher priority than indirection(*), but yes prefix(++) and indirection(*) are of equal precedence. Here the reason is not precedence but the property of postfix(++) , that is it will evaluate after the expression is evaluated. And here the expression is *ptr. That's why it first give the value and then increment, not because of precedence. I am not criticising you, but just wanted you to know the exact reason. Thanks & Regards
hi Rakesh, i think u have some confusion. please check "Pointers in C" book. and u will get solution.
Hi Neha, I don't think there is any scope of confusion, but if you say so, I'll surely check that book. Thanks
i think as first arr value 10 is assigned to *ptr & *ptr value is 10; if it is declared in the next statement as *ptr++ it's value will be incremented to 11;
i also agree with shabbir man as it *ptr++ undergoes post increment operation first expression is evaluated then in the further step it is incremented i.e in the next statement if we write another statement k man try keeping *++ptr u will get 20,10 k have a nice day