tags:

views:

223

answers:

5
 char *p="orkut" vs const char *p="orkut"

whats the difference btwn these two...

EDIT

from bjarne stroustrup,3rd edition page 90

void f()
{

char* p="plato";
p[4]='e' // error: assign to const;result is undefined

}

this kind of error cannont be general b caught until run time and implementations differ in their enforcement of this rule

Same with const char *p="plato"

Thats why iam asking the diffrence... Whats the significance of const here..

+7  A: 

The const char* variant is correct.

You should not change memory that comes from a string literal (referred to as static storage usually). It is read only memory.

The difference is that the char* variant will allow you to write the syntax to change the data that it points to by dereferencing it. What it actually does though is undefined.

//Option 1:
char *p = "orkut";
*p = 'x';//undefined behavior


//Option 2:
const char *q = "orkut";
*q = 'x';//compiling error

I would rather have option 2 happen to me.

Brian R. Bondy
Not all compilers require this, but the static data can very well end-up in read-only page/segment. So even when your compiler allows it, this can lead to wrong behavior (crash) at run-time.
EFraim
but, the questions is, 'what is the difference between these two', not 'which is correct'? :)
KevinDTimm
@KevinDTimm: Agreed see my edits.
Brian R. Bondy
Not strictly true. The `char *` variant will (more easily) allow undefined behaviour. Whether it will actually allow you to change the data is undefined. Whether the string literal is actually in read-only memory is also undefined. Both are "correct" in that they are permitted by the language and must be accepted by compilers.
Charles Bailey
@Charles: I clarified that I meant it allows you to write the syntax to do.... but clarified that may not match the actual behavior.
Brian R. Bondy
I think the answer seems more consistent now; it seemed a little contradictory before.
Charles Bailey
@Charles: Agreed! Thanks for the suggestion.
Brian R. Bondy
A: 

A declaration of const char * p means that the thing p points at is const, ie should not change. I say should not because it is possible to cast away the constness. As has been pointed out, changing a string literal is undefined and often leads to an access violation/segmentation fault.

Not this declaration is different than char * const p which means that p itself it const rather than the thing p points at.

frankc
A: 

The problem that Stroustrup discusses in what you're quoting is that in C++ a string literal will readily convert to "an rvlaue of type "pointer to char" (4.2/2 "Array-to-pointer conversion"). This is specifically so that the very common idiom of pointing a char* to a literal string wouldn't cause a bazillion programs to fail to compile. (especially when C++ was initially evolving from C).

If you can get away with declaring you pointer as char const* (or the equivalent const char*), you'll help yourself from running into problems like those described in the Stroustrup quote. However, you might well run into irritating problems using the pointer with functions that aren't 100% const correct.

Michael Burr
A: 

See this question.

Basically,

char *p="orkut";

In this case, p is supposed to be read-only, but this is not enforced by all compilers (or the standard).

BlueRaja - Danny Pflughoeft
A: 

note that more recent gcc implementations will not let you do

  char *foo = "foo";

they insist on the const (certainly in -wall -werror mode)

pm100
i should say thats for c++ not for c
pm100