confused with the char*

Discussion in 'C++' started by Yin Zhu, Dec 11, 2006.

  1. Yin Zhu

    Yin Zhu Guest

    why the answer is the second
    ?

    #include <iostream.h>

    void fn(const char *a)
    {
    cout<<"const"<<endl;
    cout<<a<<endl;
    }

    void fn(char *a)
    {
    cout<<"null"<<endl;
    cout<<a<<endl;
    }


    void main()
    {
    fn("shirui");
    }

    thanks in advance.
     
    Yin Zhu, Dec 11, 2006
    #1
    1. Advertising

  2. Yin Zhu

    frame Guest

    The function with signature "fn(const char *a)" is the one that would
    be called from main() i.e. the output would be as follows:
    const
    shirui

    By the way, I think you missed things like mention of 'std' namespace
    and main to return 'int' (but not 'void')...
     
    frame, Dec 11, 2006
    #2
    1. Advertising

  3. Yin Zhu

    Salt_Peter Guest

    Yin Zhu wrote:
    > why the answer is the second
    > ?


    its not the second version that gets called.

    >
    > #include <iostream.h>

    #include <iostream>

    >
    > void fn(const char *a)
    > {
    > cout<<"const"<<endl;
    > cout<<a<<endl;
    > }


    cout and endl do not exist. Try:

    void fn(const char *a)
    {
    std::cout << "void fn(const char *a)" << std::endl;
    std::cout << a << std::endl;
    }

    although i prefer:

    void fn(const char * const a)
    {
    std::cout << "void fn(const char * const a)" << std::endl;
    std::cout << a << std::endl;
    }

    >
    > void fn(char *a)
    > {
    > cout<<"null"<<endl;
    > cout<<a<<endl;
    > }
    >
    >
    > void main()


    void main() is illegal in C++. If you have a teacher that specifies
    otherwise, he/she should be sternly corrected. In fact, void main() was
    never, ever accepted in C++.
    Note that the
    return 0;
    statement is injected by the compiler for you if you chose to skip it.

    int main()
    {
    fn("shirui");
    }

    or

    int main()
    {
    fn("shirui");
    return 0;
    }

    is correct.

    > {
    > fn("shirui");
    > }
    >
    > thanks in advance.


    You'll note that *if* fn(char* a) was called, that would imply that
    what is at that pointer is mutable (non-constant). If you then try to
    modify the string literal "shirui" through fn(char* a), the compiler
    would generate an error because that literal is stored in an area of
    memory were variables are not modifyable / mutable.
    Thats why void fn(const char *a) is the correct call.
     
    Salt_Peter, Dec 11, 2006
    #3
  4. Yin Zhu

    Mingming Guest

    because you call the function with constant type as argument.
    "Yin Zhu дµÀ£º
    "
    > why the answer is the second
    > ?
    >
    > #include <iostream.h>
    >
    > void fn(const char *a)
    > {
    > cout<<"const"<<endl;
    > cout<<a<<endl;
    > }
    >
    > void fn(char *a)
    > {
    > cout<<"null"<<endl;
    > cout<<a<<endl;
    > }
    >
    >
    > void main()
    > {
    > fn("shirui");
    > }
    >
    > thanks in advance.
     
    Mingming, Dec 11, 2006
    #4
  5. Yin Zhu

    Noah Roberts Guest

    frame wrote:
    > The function with signature "fn(const char *a)" is the one that would
    > be called from main() i.e. the output would be as follows:
    > const
    > shirui
    >
    > By the way, I think you missed things like mention of 'std' namespace
    > and main to return 'int' (but not 'void')...


    Looks to me like the OP is using pre-standard C++. Many books used at
    schools do and many compilers are able to handle it for backward
    compatibility. The OP may not be aware of the need for std::
     
    Noah Roberts, Dec 11, 2006
    #5
  6. Yin Zhu

    Howard Guest

    "Salt_Peter" <> wrote in message
    news:...
    >


    >>
    >>
    >> void main()

    >
    > void main() is illegal in C++. If you have a teacher that specifies
    > otherwise, he/she should be sternly corrected. In fact, void main() was
    > never, ever accepted in C++.


    You'd be more correct to say that void main() was never accepted in
    "standard" C++. It's not only _accepted_ by some older C++ compilers, but
    it's actually _generated_ by some older C++ IDEs, when you initially create
    a C++ "project".

    (Yeah, yeah, get a better compiler, I know. But we could correct the error
    a little more friendly-like, dontcha think?)

    -Howard

    P.S. I'd like to see you go to class and "sternly correct" your instructor.
    That's _sure_ to get you a nice grade! :)
     
    Howard, Dec 12, 2006
    #6
  7. Mingming wrote:
    > because you call the function with constant type as argument.
    > "Yin Zhu дµÀ£º
    > "
    >
    >>why the answer is the second
    >>?
    >>
    >>#include <iostream.h>
    >>
    >>void fn(const char *a)
    >>{
    >> cout<<"const"<<endl;
    >> cout<<a<<endl;
    >>}
    >>
    >>void fn(char *a)
    >>{
    >> cout<<"null"<<endl;
    >> cout<<a<<endl;
    >>}
    >>
    >>
    >>void main()
    >>{
    >> fn("shirui");
    >>}
    >>
    >>thanks in advance.

    >
    >


    You are "confused" and there's little hope for you.
     
    Uenal S. Mutlu, Dec 17, 2006
    #7
  8. Yin Zhu

    frame Guest

    Uenal S. Mutlu wrote:

    > You are "confused" and there's little hope for you.


    Hey! don't break wind???...
    [NOTE: As long as you post this kind of stuff in this group, I shall
    follow up with this advice!]
     
    frame, Dec 17, 2006
    #8
    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. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,521
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,064
    Ian Collins
    May 9, 2006
  5. arnuld
    Replies:
    19
    Views:
    694
    Jacek Dziedzic
    Mar 30, 2007
Loading...

Share This Page