472,336 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 7606
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 ...
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate...
9
by: Gary | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p =...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const...
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference...
7
by: W Marsh | last post by:
Hi, Could anybody tell me wh the parameter "T val" is not marked const in this Stroustrup code, considering that val is not modified and not...
7
by: Luna Moon | last post by:
Hi all, I just couldn't get myself clear about the usage of "const" in front of and/or behind variables, pointers, classes, objects and...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.