Re: Help with this C snippet
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.
Re: Help with this C snippet
Its because you are getting lucky.
Are you deliberately trying to crash your program?
Re: Help with this C snippet
Quote:
Originally Posted by AnandD
i'm using TURBO C compiler on a Windows System
Code:
#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++)
Re: Help with this C snippet
Quote:
Originally Posted by stephenteh
Wouldn't you get an infinite loop ? while(*p2++ = *p1++)
Assignment expressions will give a value 0 from time to time.
Re: Help with this C snippet
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: not Best regards,
Re: Help with this C snippet
Quote:
Originally Posted by Bornish
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.....
Re: Help with this C snippet
Quote:
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 ||.
Re: Help with this C snippet
Quote:
Originally Posted by DreamShore
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) :-)
Re: Help with this C snippet
Quote:
Originally Posted by Lindley
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...
Re: Help with this C snippet
Thanx ya'll
i guess its a garbage value...
Re: Help with this C snippet
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.
Re: Help with this C snippet
Quote:
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().
Re: Help with this C snippet
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.
Re: Help with this C snippet
Quote:
Originally Posted by laserlight
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
Quote:
Originally Posted by laserlight
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: