Find the output for the following C programmain(){char *ptr = "Ramco Systems";*ptr++;printf("%sn",ptr);ptr++;printf("%sn",ptr);}explain difference between *ptr++ and ptr++

Questions by johnhu   answers by johnhu

Showing Answers 1 - 21 of 21 Answers

kalayama

  • Dec 6th, 2006
 

The output of the aboveprogram would be,amco Systemsnmco SystemsI've got some serious doubts though regarding the way in which the program is written.Why on earth would you want to prite the printf like this? printf("%sn",ptr); What is the purpose of 'n' there ? Did you intend to write n ?As far as difference between *ptr++ and ptr++ is concerned, Ideally *ptr++ should increment the contents of address ptr is pointing to and prt++ will make the pointer point to the next address in memory. Is that what you expect?NO. It doesn't work that way. literally both *ptr++ and ptr++ work the same as ++ has higher precedence than *. *ptr++ will be intrpreted as *(ptr++)Have doubts?Run the code in any compiler.The compiler I use is Dev-C++ 9.2.2 from Blood Shed developers. It is good.Cheers,Kalayama

  Was this answer useful?  Yes

kalayama

  • Dec 8th, 2006
 

Thanks.But for thse kina questions, it is always easy to run the code and then analyze. Make a lot of sense. Sometimes, all theoritical knowledge we have will not be enough. we need more knowledge. And this is how we get it.Get a good compiler friend and an IDE. It will help a lot. Which IDE/Compiler are you using?

  Was this answer useful?  Yes

Vivek

  • Jan 3rd, 2007
 

Change the program as follows :

main()
{
char *ptr = "Ramco Systems";
char temp;
temp = *ptr++;
printf("%cnn",temp);
ptr++;
printf("%sn",ptr);
}

And see the out put would be Rn and mco Systemsn ( Though I don't have any clue about the n placed in the actual problem).

Here the thing is when ++ is used as an post operator in an assignment the * operator would access the value at the pointer location first, assign it to the tem varable and then increment the pointer itself :-)

  Was this answer useful?  Yes

pvsola

  • Mar 12th, 2008
 

Hello All,

Output for this program will be:

Samco Systemsnamco Systems

Explanation:

after excuting *ptr++, value of 'R' is incremented by 1 i.e. 83 -> 'S'

Cheers!!

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions