473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 5940
Jrdman <ahmed.bo...@gmail.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...@gmail.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*******@gmail.comwrote:
>On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gmail.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...@gmail.comwrote:
On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gmail.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*********@verizon.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*****@invalid.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**********@registered.motzarella.org>,
Richard <rg****@gmail.comwrote:
>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
Richard<rg****@gmail.comwrites:
<snip>
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
Not if people use odd orders like char const *p;

There is a simple rule the everyone sing C should know. It is one of
those simple rules that is hard to write down in exact detail (I have
done in some old posts) but the gist of it is:

(1) Find the identifier (i.e. the thing being declared/defined).

(2) Read the declaration outwards from the name, always going to
right if you can and then going left when you can't. Imagine
crossing off the symbol as you voice them so you never voice a
symbol or keyword ore than once.

(3) Bracket nesting has to be respected. I.e. when you hit a ")"
going right, you must read anything left inside these brackets
(by reading to the right now) before you cross them off as done.

So for the odd:

char const *p
^ "p is a" (now we can't go right)
^ "pointer to (a)"
^ "constant"
^ "char"

char *const p
^ "p is a"
^ "constant"
^ "pointer to (a)"
^ "char"

You can say "read-only" for "const" if you prefer to get slightly
closer the real meaning.

Let's try a more complicated one:

int *const *p[6]
^ "p is an" (here we can go right so we do)
^ "array of 6"
^ "pointers to"
^ "constant"
^ "pointers to"
^ "int"

int (*f)(int)
^ "f is a" (we can't go right until the () are done)
^ "pointer to a" (the () are not done so we go right)
^ "function taking"
^ "an int"
^ "and returning"
^ "int"
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.
No need if use this rule. You need to do it a few times (with cdecl at
hand) to work out the wrinkles of how you like to say each symbol but
then you can read any C type with ease.

The trickiest part is when you are reading what the syntax calls a
type name -- the bit in the brackets of a cast and in some sizeof
expressions. Here, the name will be missing so you have to work out
where the name would be. I don't find that hard, but it helps to try
a few to get the hand of it.

[This lead to another hand rule: a C type name is a declaration with
the name missing.]

--
Ben.
Sep 23 '08 #11
Ben Bacarisse <be********@bsb.me.ukwrites:
Richard<rg****@gmail.comwrites:
<snip>
>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

Not if people use odd orders like char const *p;
cecl says this is a syntax error!
>
There is a simple rule the everyone sing C should know. It is one of
those simple rules that is hard to write down in exact detail (I have
done in some old posts) but the gist of it is:

(1) Find the identifier (i.e. the thing being declared/defined).

(2) Read the declaration outwards from the name, always going to
right if you can and then going left when you can't. Imagine
crossing off the symbol as you voice them so you never voice a
symbol or keyword ore than once.

(3) Bracket nesting has to be respected. I.e. when you hit a ")"
going right, you must read anything left inside these brackets
(by reading to the right now) before you cross them off as done.

So for the odd:

char const *p
^ "p is a" (now we can't go right)
^ "pointer to (a)"
^ "constant"
^ "char"

char *const p
^ "p is a"
^ "constant"
^ "pointer to (a)"
^ "char"

You can say "read-only" for "const" if you prefer to get slightly
closer the real meaning.

Let's try a more complicated one:

int *const *p[6]
^ "p is an" (here we can go right so we do)
^ "array of 6"
^ "pointers to"
^ "constant"
^ "pointers to"
^ "int"

int (*f)(int)
^ "f is a" (we can't go right until the () are done)
^ "pointer to a" (the () are not done so we go right)
^ "function taking"
^ "an int"
^ "and returning"
^ "int"
>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.

No need if use this rule. You need to do it a few times (with cdecl at
hand) to work out the wrinkles of how you like to say each symbol but
then you can read any C type with ease.

The trickiest part is when you are reading what the syntax calls a
type name -- the bit in the brackets of a cast and in some sizeof
expressions. Here, the name will be missing so you have to work out
where the name would be. I don't find that hard, but it helps to try
a few to get the hand of it.

[This lead to another hand rule: a C type name is a declaration with
the name missing.]
Interesting. Thanks.

--
Sep 23 '08 #12
Richard<rg****@gmail.comwrites:
Ben Bacarisse <be********@bsb.me.ukwrites:
>Richard<rg****@gmail.comwrites:
<snip>
>>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

Not if people use odd orders like char const *p;

cecl says this is a syntax error!
[...]

So it does. Looks like a bug in cdecl.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 23 '08 #13

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

Similar topics

5
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...
23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
1
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 =...
11
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...
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 const char *const normal = "\033[0m"; 11 ...
8
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) ...
10
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
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.