473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

confused with the char*

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.

Dec 11 '06 #1
7 1240
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')...

Dec 11 '06 #2

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.

Dec 11 '06 #3
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.
Dec 11 '06 #4

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

Dec 11 '06 #5

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@16g2000cwy.googlegro 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! :-)
Dec 12 '06 #6
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.

Dec 17 '06 #7
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!]

Dec 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe Cipale | last post by:
I am trying to parse a data entry in a procedure. The procedure will parse a data string on either a '/' or a ':'. My proceudre is delcared as follows: void chr_pad(char dte_str, char chr_tok) ...
2
by: Kevin C. | last post by:
Can someone explain why the file output produces all zeros? It seems to work fine in memory (e.g. passing char pointers to printf) but when I output the file, it comes out as zeros. Bookkeeping...
6
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */...
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
1
by: hello12 | last post by:
hello, I am supposed to concatenate 2 strings (words). For this the function should call malloc or calloc to get memory for the newstring(after concatenated) .I am not allowed to use the library...
2
by: bochengnever | last post by:
( ) and -are left to right in the same order . eg: struct foo { int a ; void * p; } main() { struct foo* A= malloc(sizeof(struct foo));
3
by: arnuld | last post by:
1) int i = 3; 2.) int* pi; 3.) int* pi2 = &i 4.) char* pc; 5.) char c; 6) char c2 = 'a' 7.) char* pc2 = &c2 8.) char* ps = "Stroustroup"; 9.) extern double d;
19
by: arnuld | last post by:
i am trying to understand arrays and char. Stroustrup says, this a string literal: "this is a string literal" he says it is of type /const char/. he also says, because of keeping...
4
by: mattmao | last post by:
I am moving onto the tough part in learning C:( First, about the declaration of a user defined structure: I found this syntax in my lecture notes: struct userinfo { char username; ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.