Used of using const char*

S

Sona

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
 
M

Mike Wahler

Sona said:
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
 
M

Malcolm

Sona said:
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.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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.
 
S

Sona

Thanks :) That clears it.


Sona

Emmanuel said:
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.
 
D

Deepak

Sona said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top