0
#include <stdio.h> 

void main(void) 
{ 

char* p ="sahil"; 
printf("%c,%c,%c,%c",*p,*(++p),*(p++),*p); 

} 

result:

h,h,s,h 

it should be:

s,a,a,h 

Could someone explain the result.


one more situation:

#include <stdio.h> 

void main(void) 
{ 

char p[] ="sahil";                            <<<< Change in the declaration 
printf("%c,%c,%c,%c",*p,*(++p),*(p++),*p);    <<<<Line 7 

} 

Result-In function 'main':

Line 7: error: invalid lvalue in increment 
Line 7: error: invalid lvalue in increment 

Plz explain

idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • This question appears to be off-topic because it is about multiple _frequently-asked_ questions. – devnull Mar 09 '14 at 08:15
  • Fix the bug and the mystery will go away. – David Schwartz Mar 09 '14 at 08:18
  • 1
    http://stackoverflow.com/questions/17540026/inconsistent-lvalue-required-error-when-incrementing-array-variable?rq=1 for the second part. – Mat Mar 09 '14 at 08:18
  • @devnull: as a beginner, how would you search for this? – Karoly Horvath Mar 09 '14 at 08:18
  • Do not write code that has undefined behavior. It usually looks ugly – Ed Heal Mar 09 '14 at 08:18
  • 1
    @KarolyHorvath (1) post-increment, pre-increment (2) error: invalid lvalue in increment. – devnull Mar 09 '14 at 08:19
  • @devnull: I agree with (2). searching for the error message is a beginner level task (though a lot of people fail this step). I disagree with (1), you're expecting too much.. and note that there are no relevant google hits for it, which shows how localized your query is. – Karoly Horvath Mar 09 '14 at 08:22
  • @KarolyHorvath Fair enough. I disagree with the fact that every other _beginner_ runs into the same undefined behavior. It seems more like an attempt to ask _smart questions_. – devnull Mar 09 '14 at 08:24
  • I wish they were that "smart" :) – Karoly Horvath Mar 09 '14 at 08:25
  • @KarolyHorvath Moreover, if one is familiar with the operators in question chances are that one would also be familiar with the terminology. – devnull Mar 09 '14 at 08:25
  • assuming they learnt the terminology in english... and realising that the combination of the two that causes the problems. i'm telling you, you're expecting too much ;) – Karoly Horvath Mar 09 '14 at 08:27
  • @KarolyHorvath Is it banned to ask beginner level questions here.All i had was a doubt,it could be any level as such. – Sahil Chodhary Mar 09 '14 at 09:41
  • @SahilChodhary: you've got the answer, so it's not a big deal. – Karoly Horvath Mar 09 '14 at 09:44

0 Answers0