confused with the char*

Y

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.
 
F

frame

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')...
 
S

Salt_Peter

Yin said:
why the answer is the second
?

its not the second version that gets called.
#include <iostream.h>
#include said:
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.
 
M

Mingming

because you call the function with constant type as argument.
"Yin Zhu дµÀ£º
"
 
N

Noah Roberts

frame said:
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::
 
H

Howard

Salt_Peter said:
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! :)
 
U

Uenal S. Mutlu

Mingming said:
because you call the function with constant type as argument.
"Yin Zhu дµÀ£º
"

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

frame

Uenal said:
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!]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top