468,308 Members | 1,223 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,308 developers. It's quick & easy.

what's the use of "const char*" as a parameter?

some function make the data type of it's parameters as "const char*"

why not use char or char* instead
what would be different

Jul 12 '07 #1
4 5672
Virtual_X wrote:
some function make the data type of it's parameters as "const char*"

why not use char or char* instead
what would be different
The type 'char' can only help passing a _single_ character.

The type 'char*' can help passing an array of non-constant characters.

The type 'const char*' helps passing an array of constant characters.

It means the function needs an array (often it's a C-string), and the
function promises that it will not change the contents of the array.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '07 #2
Victor Bazarov wrote:
Virtual_X wrote:
>some function make the data type of it's parameters as "const char*"

why not use char or char* instead
what would be different

The type 'char' can only help passing a _single_ character.

The type 'char*' can help passing an array of non-constant characters.

The type 'const char*' helps passing an array of constant characters.

It means the function needs an array (often it's a C-string),
Even though it usually does need one for the case of pointers to char, the
declaration doesn't mean that. The function gets a pointer to a single
char. It's just a convention between the function and the caller that this
actually points to the first element of an array.

Jul 12 '07 #3
A function which parameter is const char *, it can accept char * and
const char * type of argument.

But if function's parameter is only char *, it will give a compilation
error when you try to
pass a const char *.
On Jul 12, 11:30 pm, Rolf Magnus <ramag...@t-online.dewrote:
Victor Bazarov wrote:
Virtual_X wrote:
some function make the data type of it's parameters as "const char*"
why not use char or char* instead
what would be different
The type 'char' can only help passing a _single_ character.
The type 'char*' can help passing an array of non-constant characters.
The type 'const char*' helps passing an array of constant characters.
It means the function needs an array (often it's a C-string),

Even though it usually does need one for the case of pointers to char, the
declaration doesn't mean that. The function gets a pointer to a single
char. It's just a convention between the function and the caller that this
actually points to the first element of an array.

Jul 13 '07 #4
su****************@gmail.com wrote:
A function which parameter is const char *, it can accept char * and
const char * type of argument.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Jul 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by s88 | last post: by
4 posts views Thread by C. J. Clegg | last post: by
20 posts views Thread by liujiaping | last post: by
6 posts views Thread by Darin Johnson | last post: by
7 posts views Thread by W Marsh | last post: by
7 posts views Thread by Luna Moon | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.