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

V

Virtual_X

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

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

Victor Bazarov

Virtual_X said:
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
 
R

Rolf Magnus

Victor said:
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.
 
S

subscription.gupta

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

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top