Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > confused with the char*

Reply
Thread Tools

confused with the char*

 
 
Yin Zhu
Guest
Posts: n/a
 
      12-11-2006
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.

 
Reply With Quote
 
 
 
 
frame
Guest
Posts: n/a
 
      12-11-2006
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')...

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-11-2006

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.

 
Reply With Quote
 
Mingming
Guest
Posts: n/a
 
      12-11-2006
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.


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      12-11-2006

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::

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-12-2006

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


>>
>>
>> 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!


 
Reply With Quote
 
Uenal S. Mutlu
Guest
Posts: n/a
 
      12-17-2006
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.

 
Reply With Quote
 
frame
Guest
Posts: n/a
 
      12-17-2006
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!]

 
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
confused! Dennis J. Tuchler Firefox 4 05-19-2005 12:04 AM
Thunderbird 1.0 got confused with news 'Captain' Kirk DeHaan Firefox 0 12-24-2004 08:16 PM
Utterly confused - web, sql tables what to use for development? RichGK ASP .Net 1 07-31-2004 05:16 AM
Confused and bewildered. DJ Miller ASP .Net 4 09-04-2003 06:10 PM
Confused. Need Help! Cobra Pilot Perl 1 07-22-2003 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57