question about vc++.net compiler

Discussion in 'C Programming' started by nick, Nov 18, 2005.

  1. nick

    nick Guest

    #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!
    nick, Nov 18, 2005
    #1
    1. Advertising

  2. nick

    A.A Guest

    printf("%s\n", cptr);
    or printf("%c\n", *cptr);
    A.A, Nov 18, 2005
    #2
    1. Advertising

  3. nick

    A.A Guest

    printf("%s\n", cptr);
    or printf("%c\n", *cptr);
    A.A, Nov 18, 2005
    #3
  4. nick

    Mike Wahler Guest

    "nick" <> wrote in message
    news:dljn1p$4q6$...
    > #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
    Mike Wahler, Nov 18, 2005
    #4
  5. On Fri, 18 Nov 2005 13:00:09 +0800, in comp.lang.c , nick
    <> 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 =----
    Mark McIntyre, Nov 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,119
  2. Jack Wright
    Replies:
    5
    Views:
    614
    Shiv Kumar
    Jan 19, 2004
  3. Ram
    Replies:
    0
    Views:
    2,819
  4. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    432
    Frederik Hertzum
    May 17, 2004
  5. RickMuller
    Replies:
    4
    Views:
    693
    Alexey Shamrin
    Mar 26, 2005
Loading...

Share This Page