473,404 Members | 2,137 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,404 software developers and data experts.

Used of using const char*

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
6 4727
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

"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
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
Mike Wahler wrote:
*pc = 'X'; /* Undefined behavior, tries to modify string literal */


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

Nov 13 '05 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: qazmlp | last post by:
I was using the following code to convert the string to lowercase. string foo = "Some Mixed Case Text"; transform(foo.begin(), foo.end(), foo.begin(), tolower); I thought the above code is...
7
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
0
by: Vidyasagara Guntaka | last post by:
Hi, I'm not able to compile libpq for Windows environment using the 8.0.0 beta 1 source tree. I got the following errors (The entire compilation output is listed below): ...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
4
by: uche | last post by:
Hello, I am having a problem with my code in C++/C. I have struggled for a while and cannot find the solution to the problem... Here is the compiled..output: In file included from disk.cpp:8:...
9
by: chikkubhai | last post by:
Why is the result different for the following set of two code snippets Code without using this pointer #include <string> #include <iostream> using namespace std; struct X { private:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.