By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

Used of using const char*

P: n/a
Hi,

What's the advantage/disadvantage of using a "const char*" over a
"char*" ? I read some place that char* are string literals that on some
machines are stored in a read only memory and cannot be modified... does
that apply on const char*?

thanks

Sona

Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sona <so**********@nospam.com> wrote in message
news:3f********@clarion.carno.net.au...
Hi,

What's the advantage/disadvantage of using a "const char*" over a
"char*" ?
It's not a question of advantages/disadvantages. It's a
question of using what's appropriate.
I read some place that char* are string literals
Wherever you read that, stop reading there. That's
completely false. 'char*' expresses a type, 'pointer-to-char'.
It is not a 'string literal'. A string literal looks like this:

"I am a string literal".
that on some
machines are stored in a read only memory
String literals are stored wherever the compiler decides
to store them. The 'physical' or operating system-defined
'attributes' of such storage is irrelevant to the language.
The language only states that attempts to modify a string
literal give 'undefined behavior'.

and cannot be modified...
Some platforms might allow such modification, others might
not. Others might crash. Etc. You cannot know from a
language point of view. It's simply 'undefined behavior'.
does
that apply on const char*?


char *literal = "string literal";
/* a pointer-to-char, initialized with the address
of the first character of the literal "string literal" */

char array[] = "Hello world";
/* an array of twelve characters */

char *p = array;
/* a pointer-to-char, initialized with the address of the
first character of the array named 'array'. */

const char *pc = array;
/* a pointer-to-const-char, initialized with the address of the
first character of the array named 'array'. */

*p = 'X'; /* Change what 'p' points to, OK */

*pc = 'X'; /* Change what 'pc' points to, Error, 'pc' points to
const char */

p = literal; /* assign to 'p' the address of the first character
of the string literal */

pc = literal; /* assign to 'pc' the address of the first character
of the string literal */
*p = 'X'; /* Undefined behavior, tries to modify string literal */
*pc = 'X'; /* Undefined behavior, tries to modify string literal */
Whch C book(s) are you reading? Perhaps you need better ones.
See www.accu.org for peer reviews.

-Mike

Nov 13 '05 #2

P: n/a

"Sona" <so**********@nospam.com> wrote in message

What's the advantage/disadvantage of using a "const char*" over a
"char*" ? I read some place that char* are string literals that on some
machines are stored in a read only memory and cannot be modified... > does that apply on const char*?

You use the const qualifier for a string that is not modified. The usual
place to do this is in a function parameter list.

Should you accidentally try to modify the passed string, the compiler will
warn you of your error. It is also a help to other programmers in
documenting the code.
In
void copystring(char *s1, const char *s2)

despite the poor choice of parameter names it is obvious that s1 is the
destination string.
Nov 13 '05 #3

P: n/a
In 'comp.lang.c', Sona <so**********@nospam.com> wrote:
What's the advantage/disadvantage of using a "const char*" over a
"char*" ? I read some place that char* are string literals that on some
machines are stored in a read only memory and cannot be modified... does
that apply on const char*?


This is misleading.

char * is nothing but a pointer. Where does it point depends on how it has
been initialized (or assigned).

{
char *a; /* invalid value (undetermined) */
char *b = NULL; /* invalid value */
char *c = "hello"; /* string literal [1] */
char d[] = "hello";
char *e = d; /* array of char [2] */
char const *f = d; /* read-only array of char [3] */
}

[1] A string literal can be writable or not. To stay portable, and to avoid
undefined behaviours, better de define it 'const':

{
char const *c = "hello";
}

[2] The 'd' array of char is writable. The 'e' pointer is safe and allows a
read/write acces to the array.

[3] The 'f' pointer is safe and allows a read-only acces to the array.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #4

P: n/a
Mike Wahler wrote:
*pc = 'X'; /* Undefined behavior, tries to modify string literal */


That's an error, pc is const char*.

Nov 13 '05 #5

P: n/a
Thanks :) That clears it.
Sona

Emmanuel Delahaye wrote:
In 'comp.lang.c', Sona <so**********@nospam.com> wrote:

What's the advantage/disadvantage of using a "const char*" over a
"char*" ? I read some place that char* are string literals that on some
machines are stored in a read only memory and cannot be modified... does
that apply on const char*?

This is misleading.

char * is nothing but a pointer. Where does it point depends on how it has
been initialized (or assigned).

{
char *a; /* invalid value (undetermined) */
char *b = NULL; /* invalid value */
char *c = "hello"; /* string literal [1] */
char d[] = "hello";
char *e = d; /* array of char [2] */
char const *f = d; /* read-only array of char [3] */
}

[1] A string literal can be writable or not. To stay portable, and to avoid
undefined behaviours, better de define it 'const':

{
char const *c = "hello";
}

[2] The 'd' array of char is writable. The 'e' pointer is safe and allows a
read/write acces to the array.

[3] The 'f' pointer is safe and allows a read-only acces to the array.


Nov 13 '05 #6

P: n/a
Sona <so**********@nospam.com> wrote in message news:<3f********@clarion.carno.net.au>...
Hi,

What's the advantage/disadvantage of using a "const char*" over a
"char*" ? I read some place that char* are string literals that on some
machines are stored in a read only memory and cannot be modified... does
that apply on const char*?

thanks

Sona

There are some time when u do not want to change some values stored in
a predefined place.
That you do by placing the key word "const <TYPE NAME>". This is just
an indication to the compiler that if it find any place in the code
where this constraints is violated a warning will be given by the
compiler.
Please note that it is not an error condition. You can anyway ASSIGN
an pointer to it and can modify the value.But it is your fault.
Compiler will anyway warn you. This is not have any relation to run
time environment.
Generally this is done in passing some pointer to function which may
modify its content.Then the function prototype and defination must be
including the "const"
as the type modifier.

The string literals are generally stored in text area of the
object/executable file which maps to an read-only memory section at
run time.
So if any attempt is made to modify the value at that place, u will be
getting the invalid memory reference error which is segmention fault
so a core file will be produced on unix systems.
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.