Click to See Complete Forum and Search --> : Help with this C snippet


AnandD
June 4th, 2008, 07:59 AM
i'm using TURBO C compiler on a Windows System


#include <stdio.h>
main()
{
char *p1 = "New";
char *p2;
p2 = (char *)malloc(20);
while(*p2++ = *p1++)
;
printf("\n%s",p2)
}


o/p :
Blank screen

From my understanding p2 is pointing to one position after '\0' character.
But why is the o/p appearing blank when it shud be a garbage value.
I tried it on two different pc's running TURBO C but still got the same result.
or is the blank character a garbage value..

Lindley
June 4th, 2008, 08:11 AM
Depends on your compiler settings. Under some modes, extra 0-padding is added even if you don't ask for it.

My guess is that you're seeing garbage, the first char of which just happens to be 0.

Duoas
June 4th, 2008, 08:13 AM
Its because you are getting lucky.

Are you deliberately trying to crash your program?

stephenteh
June 4th, 2008, 08:37 AM
i'm using TURBO C compiler on a Windows System


#include <stdio.h>
main()
{
char *p1 = "New";
char *p2;
p2 = (char *)malloc(20);
while(*p2++ = *p1++)
;
printf("\n%s",p2)
}


o/p :
Blank screen

From my understanding p2 is pointing to one position after '\0' character.
But why is the o/p appearing blank when it shud be a garbage value.
I tried it on two different pc's running TURBO C but still got the same result.
or is the blank character a garbage value..

Wouldn't you get an infinite loop ? while(*p2++ = *p1++)

DreamShore
June 4th, 2008, 08:44 AM
Wouldn't you get an infinite loop ? while(*p2++ = *p1++)

Assignment expressions will give a value 0 from time to time.

Bornish
June 4th, 2008, 08:46 AM
There's a difference between ++p and p++!
That's why p2 points to the string terminating NULL char. Learn to debug.
Also, I believe it should be: printf("%s\n",p2)notprintf("\n%s",p2) Best regards,

Lindley
June 4th, 2008, 08:52 AM
There's a difference between ++p and p++!
That's why p2 points to the string terminating NULL char.

I think ++p *would* have it pointing to the terminating null, but p++ does not.....

DreamShore
June 4th, 2008, 08:54 AM
There's a difference between ++p and p++!
That's why p2 points to the string terminating NULL char. Learn to debug.
And try to debug.

All I recall there are two operator can break the evaluation of expression && and ||.

stephenteh
June 4th, 2008, 08:59 AM
Assignment expressions will give a value 0 from time to time.

Yes you are right, just verified it in ollydbg :-). And for the op's code the loop will test the value from p1 and break the loop when it's null. Learnt a new thing today (at least for me) :-)

Bornish
June 4th, 2008, 08:59 AM
I think ++p *would* have it pointing to the terminating null, but p++ does not.....True... my bad. :D
All he needs to do is to memset the p2 with some other char before the while...

AnandD
June 4th, 2008, 02:35 PM
Thanx ya'll

i guess its a garbage value...

Zaccheus
June 5th, 2008, 03:28 AM
Pehaps I'm being thick but I can't see anything with the OP code except the printf statement at the end:

1) Missing ; after function call.
2) Some systems don't do any output untill the next \n.

laserlight
June 5th, 2008, 03:38 AM
Pehaps I'm being thick but I can't see anything with the OP code except the printf statement at the end:
The most fundamental problem is that p2 itself is changed by the loop, but that was not accounted for. Aside from that and what you mentioned, other problems include the fact that <stdlib.h> is not included, main() is not declared as explicitly returning an int, p1 is not declared as pointing to a const char, and there is no free() to match the malloc().

Duoas
June 5th, 2008, 08:51 AM
Thanks laserlight.

Yes, at the end of the loop, p2 points to one-past the null terminator, so all bets are off when you try to printf() it.

Sorry, I suppose I could have been much more helpful about that in the first post.

Zaccheus
June 6th, 2008, 11:39 AM
The most fundamental problem is that p2 itself is changed by the loop, but that was not accounted for.

Oh yes, so it is.
:D

Aside from that and what you mentioned, other problems include the fact that <stdlib.h> is not included, main() is not declared as explicitly returning an int, p1 is not declared as pointing to a const char, and there is no free() to match the malloc().
:thumb: