An update on Failing C program

Actual Post

Now coming to analysis part of the program

#include <stdio.h>

int main(void){
    char wcrs[20]={'w','h','o',0,'c','a','r','e','s',0,0};
    int temp=0;
    char *pChar=wcrs;
    while((*pChar +*(++pChar)) != 0)//Problem lies here
    {
        temp++;
    }
    printf("%d\n",temp);
    return 0;
}

C Programming Problem with pointers

Problem is with the pointer operation, all people got answer 9 I hope, even the intention is also the same, i.e. count till two nulls occurred and display the result.

But the problem doesn't occur if you just do
gcc -Wall trail.c -o trail

occurs when
gcc -Wall -O trail.c -o trail

I tried searching in the net to find out what's wrong but couldn't find out but compiler optimized this code
while((*pChar +*(++pChar)) != 0) became while((*(++pChar)) != 0).
Now the solution is just making  pChar volatile.
But I didn't still find the actual optimization tag and the reason.
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
I tried all the optimizations but not able to succeed or find the optimization. If any one knows help me out.

No comments:

Post a Comment

తెలుగులో వ్రాయడానికి http://www.google.com/ime/transliteration/ ఉపయోగించండి.