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

Home Posts Topics Members FAQ

const char*

hi,
in some function prototypes why they use const char* instead of
const char what's the need for const here is?
if we must use const
and why they don't use char *const instead because it's the right way
to designate a const string (i think)?
Sep 22 '08 #1
12 5983
Jrdman <ahmed.bo...@gm ail.comwrote:
hi,
in some function *prototypes why they use const *char*
instead of const char
Because a single character passed by value is not the same
thing as a pointer to an array of characters.
what's the need for const here is?
In const char * it signifies that the characters being
pointed to are const.
if we must use const and why they don't use char *const
instead because it's the right way
to designate a const string (i think)?
What do you think of when you say 'constant string'?
Is it a string that never moves, or a string that
shouldn't change?

Strictly speaking, const means 'can't write'. It doesn't
mean 'can't change'. The purpose of const is to guard
against attempting to change data you do not have (or
give yourself) permission to change.

--
Peter
Sep 22 '08 #2
On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gm ail.comwrote:
hi,
in some function *prototypes why they use const *char* instead of
const char what's the need for const here is?
1.
void display(const char*);
int main(void)
{
display("hello" ); // In this case, you cannot use "const char".

return 0;
}
2.
const char* tString = "world"; //This means the string pointed to
by tString is constant
and cannot be modified.
Sep 23 '08 #3
On Mon, 22 Sep 2008 21:25:01 -0700 (PDT), santoshsy
<sa*******@gmai l.comwrote:
>On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gm ail.comwrote:
>hi,
in some function *prototypes why they use const *char* instead of
const char what's the need for const here is?

1.
void display(const char*);
int main(void)
{
display("hello" ); // In this case, you cannot use "const char".
It is certainly permissible to pass a non-constant string to a
function expecting a pointer to const char (e.g., strcmp). What did
you really mean to say?
>
return 0;
}
2.
const char* tString = "world"; //This means the string pointed to
by tString is constant
and cannot be modified.
The only restriction is that the object pointed to cannot be modified
by dereferencing this pointer.

char arr[] = "world";
const char *p1 = arr;
char *p2 = arr;
p1[2] = 'u'; /* invalid */
p2[2] = 'u'; /* should be fine */

--
Remove del for email
Sep 23 '08 #4
On 23 Sep, 06:50, Barry Schwarz <schwa...@dqel. comwrote:
On Mon, 22 Sep 2008 21:25:01 -0700 (PDT), santoshsy
<santos...@gmai l.comwrote:
On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gm ail.comwrote:
in some function *prototypes why they use const *char* instead of
const char what's the need for const here is?
note the OP asks why you can't substitute "const char" for
"const char*". Because one's a pointer and one isn't...
He may have meant "char*" for "const char*"

1.
void display(const char*);
int main(void)
{
* display("hello" ); *// In this case, you cannot use "const char".

It is certainly permissible to pass a non-constant string to a
function expecting a pointer to const char (e.g., strcmp). *What did
you really mean to say?
exactly what he said!

return 0;
}
2.
* const char* tString = "world"; *//This means the string pointedto
by tString is constant
* * * * * * * * * * * * * * * * * *and cannot be modified.

The only restriction is that the object pointed to cannot be modified
by dereferencing this pointer.

* * char arr[] = "world";
* * const char *p1 = arr;
* * char *p2 = arr;
* * p1[2] = 'u'; /* invalid */
* * p2[2] = 'u'; /* should be fine */

--
Nick Keighley

"Merely corroborative detail, intended to give artistic
verisimilitude
to an otherwise bald and unconvincing narrative."
W.S.Gilbert
Sep 23 '08 #5
Jrdman wrote:
hi,
in some function prototypes why they use const char* instead of
const char what's the need for const here is?
Because 'const char' designates a single character whose value cannot be
changed, while "const char*" means a pointer to a character which cannot
be changed; a character which is often the first one of a
null-terminated string of characters.
if we must use const
and why they don't use char *const instead because it's the right way
to designate a const string (i think)?
When 'const' appears before the '*', it means that the thing pointed-at
is const. When 'const' appears after the *, it means that the pointer
itself is const. Maybe this example will make it clear:

void func(const char *p, char * const q)
{
*p = 'c'; // Constraint violation
*q = 'c'; // Permitted
p = "Hello"; // Permitted
q = "Hello"; // Constraint violation
}

Which one should be used depends upon what the function needs to do with
the pointer.
Sep 23 '08 #6

James Kuyper <ja*********@ve rizon.netwrites :
When 'const' appears before the '*', it means that the thing
pointed-at is const. When 'const' appears after the *, it means that
the pointer itself is const. Maybe this example will make it clear:

void func(const char *p, char * const q)
{
*p = 'c'; // Constraint violation
*q = 'c'; // Permitted
p = "Hello"; // Permitted
q = "Hello"; // Constraint violation
}

Which one should be used depends upon what the function needs to do
with the pointer.
Nice example.

And don't forget

const char * const *p;

Here cdecl comes in handy. e.g

rrob@debian:~$ cdecl explain "const char * const *p;"

declare p as pointer to const pointer to const char

frankly I cant even begin to figure out if that is right ...
Sep 23 '08 #7
On Tue, 23 Sep 2008 12:02:27 +0000, James Kuyper wrote:
When 'const' appears before the '*', it means that the thing pointed-at
is const. When 'const' appears after the *, it means that the pointer
itself is const. Maybe this example will make it clear:

void func(const char *p, char * const q)
{
*p = 'c'; // Constraint violation
*q = 'c'; // Permitted
p = "Hello"; // Permitted
q = "Hello"; // Constraint violation
}

Which one should be used depends upon what the function needs to do with
the pointer.


hey James, it took me 12 months to understand it :). No, I am serious.
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 23 '08 #8
arnuld <su*****@invali d.addresswrites :
>On Tue, 23 Sep 2008 12:02:27 +0000, James Kuyper wrote:
>When 'const' appears before the '*', it means that the thing pointed-at
is const. When 'const' appears after the *, it means that the pointer
itself is const. Maybe this example will make it clear:

void func(const char *p, char * const q)
{
*p = 'c'; // Constraint violation
*q = 'c'; // Permitted
p = "Hello"; // Permitted
q = "Hello"; // Constraint violation
}

Which one should be used depends upon what the function needs to do with
the pointer.

hey James, it took me 12 months to understand it :). No, I am serious.
I wonder if its as simple as "the const element is the one to the right
of the const keyword"?

const char *p : the char is const
char * const q : q is const

I would be a liar if I didn't say I always highlight such and call up
cdecl in my editor to be sure to be sure. But then I always have to
check the syntax for typedef and struct combos to see which is the type
and which is the object created .... (primarily since I generally never use
typedef unless its in other peoples code ans am always language hopping
which causes its own problems when coming back to C)
Sep 23 '08 #9
In article <gb**********@r egistered.motza rella.org>,
Richard <rg****@gmail.c omwrote:
>void func(const char *p, char * const q)
{
*p = 'c'; // Constraint violation
*q = 'c'; // Permitted
p = "Hello"; // Permitted
q = "Hello"; // Constraint violation
}
>Nice example.

And don't forget

const char * const *p;

Here cdecl comes in handy. e.g

rrob@debian: ~$ cdecl explain "const char * const *p;"

declare p as pointer to const pointer to const char

frankly I cant even begin to figure out if that is right ...
It's not that hard... Just as "char *p" can be read as saying "*p is
a char", so "const char *p" can be read as "*p is a const char",
and "char * const q" as "* (const q)" is a char". So in the first
case it's *p that's const, and in the second it's q.

So in "const char * const *p", *p (which is const) is a pointer to
a const char. Note that p itself isn't const.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Sep 23 '08 #10

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

Similar topics

5
22031
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate example). int foo(const char* argv) { return 0; } int main(int argc, char* argv) {
23
6602
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
1
3908
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; I think the idea at play here is whether the pointer is constant or not ?
11
813
by: Paul Emmons | last post by:
In writing a function similar to strstr(), I'm calling both of the arguments "const char *". My compiler (gcc) complains "warning: return discards qualifiers from pointer target type" unless I have the function return "const char*" as well (rather than just "char *"). In turn, any pointer receiving this value when calling the function must also be "const char *" or the compiler will complain, "warning: assignment discards qualifiers...
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 }
8
10086
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) When I asked why, I was told that it facilitated calling it as:
10
5309
by: dwaach | last post by:
Hi, I am trying to compile the following program, #include <iostream> using namespace std; typedef char* CHAR; typedef const CHAR CCHAR;
42
32146
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
9
10519
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
0
8325
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
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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
8518
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
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
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.