Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question about vc++.net compiler

Reply
Thread Tools

question about vc++.net compiler

 
 
nick
Guest
Posts: n/a
 
      11-18-2005
#include <stdio.h>

#include <string.h>
int main(void)
{
char message[]="Welcome to C Programming!";
char *cptr;
cptr = message + 5;
printf("%c\n", cptr);
return 0;
}

the above code, when i put it in VC++.NET, it can't display properly.
but when i put it in MinGW Developer Studio, it can show the result 'm'.
why?

thanks!
 
Reply With Quote
 
 
 
 
A.A
Guest
Posts: n/a
 
      11-18-2005
printf("%s\n", cptr);
or printf("%c\n", *cptr);

 
Reply With Quote
 
 
 
 
A.A
Guest
Posts: n/a
 
      11-18-2005
printf("%s\n", cptr);
or printf("%c\n", *cptr);

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-18-2005

"nick" <(E-Mail Removed)> wrote in message
news:dljn1p$4q6$(E-Mail Removed)...
> #include <stdio.h>
>
> #include <string.h>
> int main(void)
> {
> char message[]="Welcome to C Programming!";
> char *cptr;
> cptr = message + 5;
> printf("%c\n", cptr);
> return 0;
> }
>
> the above code, when i put it in VC++.NET, it can't display properly.


There is no 'properly'. The behavior is undefined.
%c is for type 'char' not 'char*'.

Perhaps you meant:
printf("%c\n", *cptr);

> but when i put it in MinGW Developer Studio, it can show the result 'm'.
> why?


Undefined behavior can be manifest as anything.

-Mike


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-19-2005
On Fri, 18 Nov 2005 13:00:09 +0800, in comp.lang.c , nick
<(E-Mail Removed)> wrote:

>#include <stdio.h>
>
>#include <string.h>
>int main(void)
>{
>char message[]="Welcome to C Programming!";
>char *cptr;
>cptr = message + 5;
>printf("%c\n", cptr);
>return 0;
>}
>
>the above code, when i put it in VC++.NET, it can't display properly.
>but when i put it in MinGW Developer Studio, it can show the result 'm'.
>why?


%c prints a character. The 5th character of message is 'm' which is
presumably what you were expecting.

However cptr is a pointer. Printing the pointer with %c is undefined
behaviour, and the result could be anything (often it will be the
value of the address, converted to a char). So, if you got 'm' on
Mingw, it was coincidence.

Change the printf to *cptr, to get the value stored at cptr.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Why is a JIT compiler faster than a byte-compiler RickMuller Python 4 03-26-2005 04:30 PM
Compiler compiler with C++ as output Andrey Batyuck C++ 3 05-17-2004 08:17 PM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments