Jump to content
ChetanaSforum
mapgdgin

difficult question indeed

Recommended Posts

hi everybody out there,plz explain me how this code worksmain(){int i=300;char *ptr=&i;*++ptr=2; //what does this stmt mean?printf("%c",*ptr);}o/p:556

Share this post


Link to post
Share on other sites

hi everybody out there,plz explain me how this code worksmain(){int i=300;char *ptr=&i;*++ptr=2; //what does this stmt mean?printf("%c",*ptr);}o/p:556

This code will not run on VC++, as it will not allow such pointer conversions because they are illegal.However, on turbo c++, it will show you a warning message regarding this.Whatsoever, you are doing the wrong things.....Always run your program on VC++, and its a suggestion.If you want to know that whats *++ptr is doing, then it will first increment the value of ptr (note that it will not increment the value pointed out by ptr pointer) and then store 2 at that position. *ptr will show you the character with respect to the interger value (treating it as ascii value).My o/p doesnot matches your o/p. You showed it 556, where as i am getting a special character.

Share this post


Link to post
Share on other sites

This code will not run on VC++, as it will not allow such pointer conversions because they are illegal.However, on turbo c++, it will show you a warning message regarding this.Whatsoever, you are doing the wrong things.....Always run your program on VC++, and its a suggestion.If you want to know that whats *++ptr is doing, then it will first increment the value of ptr (note that it will not increment the value pointed out by ptr pointer) and then store 2 at that position. *ptr will show you the character with respect to the interger value (treating it as ascii value).My o/p doesnot matches your o/p. You showed it 556, where as i am getting a special character.

helloactually there is an error over there it requires type casting since we are assignig the address of integer to the pointer pointing the character

Share this post


Link to post
Share on other sites

It's a perfectly valid "C" code.

 

Output of

printf("%c",*ptr);

will be some character(varying from machine to machine) whose ascii value is 2.

 

Output of

printf("%d",*ptr);

 

will be 2 always.

 

Output of

printf("%d",*((int*)(ptr-1)));

 

Will be 556.

Share this post


Link to post
Share on other sites

It's a perfectly valid "C" code.

 

Output of

printf("%c",*ptr);

will be some character(varying from machine to machine) whose ascii value is 2.

 

Output of

printf("%d",*ptr);

 

will be 2 always.

 

Output of

printf("%d",*((int*)(ptr-1)));

 

Will be 556.

Hello Sunny,

 

Problem is not with the printf statement. Real problem lies in the statement "char *ptr=&i;". This is an illegal pointer conversion. Assigning an integer variable address to a pointer to char. I hope you will get tha point.

 

Prabhat

Share this post


Link to post
Share on other sites

Real problem lies in the statement "char *ptr=&i;". This is an illegal pointer conversion. Assigning an integer variable address to a pointer to char.

illegal is a very strong word. I would rather say it's conversion between incompatible types and I never denied that.

 

I said it's a valid "C" code and I still say that.

 

Problem is not with the printf statement.

Again, I never said there is a problem with printf statement. I simply explained the OP the working of the program.

 

Hope you get it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×