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.