|
-
June 4th, 2008, 07:59 AM
#1
Help with this C snippet
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..
-
June 4th, 2008, 08:11 AM
#2
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.
-
June 4th, 2008, 08:13 AM
#3
Re: Help with this C snippet
Its because you are getting lucky.
Are you deliberately trying to crash your program?
-
June 4th, 2008, 08:37 AM
#4
Re: Help with this C snippet
 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++)
-
June 4th, 2008, 08:44 AM
#5
Re: Help with this C snippet
 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.
Nope
-
June 4th, 2008, 08:46 AM
#6
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,
-
June 4th, 2008, 08:52 AM
#7
Re: Help with this C snippet
 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.....
-
June 4th, 2008, 08:54 AM
#8
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.
And try to debug.
All I recall there are two operator can break the evaluation of expression && and ||.
Nope
-
June 4th, 2008, 08:59 AM
#9
Re: Help with this C snippet
 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) :-)
-
June 4th, 2008, 08:59 AM
#10
Re: Help with this C snippet
 Originally Posted by Lindley
I think ++p *would* have it pointing to the terminating null, but p++ does not.....
True... my bad.
All he needs to do is to memset the p2 with some other char before the while...
-
June 4th, 2008, 02:35 PM
#11
Re: Help with this C snippet
Thanx ya'll
i guess its a garbage value...
-
June 5th, 2008, 03:28 AM
#12
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.
-
June 5th, 2008, 03:38 AM
#13
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:
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().
-
June 5th, 2008, 08:51 AM
#14
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.
-
June 6th, 2008, 11:39 AM
#15
Re: Help with this C snippet
 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.
 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().
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|