KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying my hands on a C pointer literature. In one of the illustrations, I encountered the following code. # include <stdio.h> int main() { static int a[]={0,1,2,3,4}; static int *p[]={a, a+1, a+2, a+3, a+4}; int **ptr; ptr =p; **ptr++; printf("%d %d %d\n", ptr-p, *ptr-a, **ptr); *++*ptr; printf("%d %d %d\n", ptr-p, *ptr-a, **ptr); ++**ptr; printf("%d %d %d\n", ptr-p, *ptr-a, **ptr); return 0; } I receive the output as. 1 1 1 1 2 2 1 2 3 I am facing a problem in justifying this output. I made lot of boxes on a copy for easy grasp of the problem. I am able to justify the output 1 1 1 , my trouble starts with the statement, *++*ptr . Since, a unary operators are executed from right to left. So, *ptr would be tackled first, then the value at ptr would be incremented. After this increment, I am not sure what happens, the book says that somehow p is also incremented to point to the next element in this array. The output 1 2 2 can only be achieved through the increment of p . I am not sure that this kind of question fits exactly on stackoverflow. I tried my best, wasted at least 10 pages with boxes drawn over them. Any clarification would be appreciated.
Tags (comma-separated)
Save Edits
Cancel