473,657 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8390
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.c om/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
6481
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 const char *const normal = "\033[0m"; 11 printf("%sHello World%s\n", green, normal); 12 return 0; 13 }
4
6075
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 memory, etc. because they will generate multiple definition errors if the header file is #include'd in more than one code file. The answer was that constants have internal linkage unless declared extern, so it's OK. So, you can put something like...
9
1269
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 the following code, can we change the contents of the const pointer (i.e. t)? I got a segmentation fault in the last for loop. I wrote the code in c++, but the language is not the point, right? :) Thanks in advance! #include <iostream>
26
11696
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
20
3538
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 char str4 = "abc"; const char* str5 = "abc";
6
5580
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 parameters to a primitive type. This is for normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
7
2077
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 non- const methods called? template<class C, class Tint count(const C&v, T val) { typename C::const_iterator i = find(v.begin(), v.end(), val); int n = 0;
7
1922
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 functions... It's too confusing... any good clear article/tutorial that can help me? Thanks a lot!
7
3038
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 that looks sensible. There's an ugly solution, but surely this is possible? I could define an operator<< but for various reasons, I really want to convert to a 'const char*' (to embed into a string which becomes part of a window's caption, etc).
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.