string literals question

C

copx

I've noticed that it's valid to pass a string literal (like "test") to a
function that expects a const char *. Does this mean that in C a string
literal is automatically converted to a const char * if I pass it to a
function?

TIA,
copx
 
M

Malcolm

copx said:
I've noticed that it's valid to pass a string literal (like "test") to a
function that expects a const char *. Does this mean that in C a string
literal is automatically converted to a const char * if I pass it to a
function?
The function

void foo(const char *str)

may not modify the string pointed to be str. However it is legal to pass it
either a constant or a variable string. This makes sense. Say foo() prints
out the string on a teletype. Obviously we want to be able to print out
either constant strings or strings entered by the user or calculated at
runtime, however we wouldn't need to modify the string just to print it out.

Now consider the function bar

void bar(char *str)
{
str[0] = 'x';
}

This function modifies the input, so it cannot be const.

const char *string = "Hello";
/* illegal */
bar(string);


char string[32] = "Hello";
/* legal */
bar(string);

/* legal but should not be legal */
bar("Hello");

The compiler will allow you to pass the string literal "Hello" to bar(), but
you will then get undefined behaviour when you try to modify the string.
This is a quirk, and is for backwards compatibility. Originally there was no
"const" keyword in C, and enforicing the rule that string literals are const
qualified would have meant breaking a lot of existing and otherwise
perfectly good code.
 
C

Chris Torek

I've noticed that it's valid to pass a string literal (like "test") to a
function that expects a const char *. Does this mean that in C a string
literal is automatically converted to a const char * if I pass it to a
function?

See the comp.lang.c FAQ, questions 1.32, section 6, and question 11.8b.
 
C

CBFalconer

copx said:
I've noticed that it's valid to pass a string literal (like
"test") to a function that expects a const char *. Does this
mean that in C a string literal is automatically converted to
a const char * if I pass it to a function?

Yes, except it isn't flagged as a const, so you can pass it as a
non-const parameter too. This is usually an error, either on your
part or in the prototype for the function. You can detect these
(in gcc) with -Wwrite-strings.

BTW, it isn't converted, that is what the string literal actually
is.
 
B

Ben Pfaff

Malcolm said:
The function

void foo(const char *str)

may not modify the string pointed to be str.

Unfortunately, it may. If the function casts away `const', then
it may write into `str'. As long as `str' is in modifiable
storage, this is perfectly legitimate according to the standard.

However, `const' is generally considered a sort of contract
between the caller of the function and the implementor of the
function. It is in bad taste to violate the contract by writing
into an argument whose parameter is declared `const'.
 
J

Joe Wright

CBFalconer said:
Yes, except it isn't flagged as a const, so you can pass it as a
non-const parameter too. This is usually an error, either on your
part or in the prototype for the function. You can detect these
(in gcc) with -Wwrite-strings.

BTW, it isn't converted, that is what the string literal actually
is.

No. A string literal is an array of char. You know that.
 
O

Old Wolf

CBFalconer said:
BTW, it isn't converted, that is what the string literal actually
is.

It is actually a (const char []), it is converted to a (const char *).
 
J

Jack Klein

CBFalconer said:
BTW, it isn't converted, that is what the string literal actually
is.

It is actually a (const char []), it is converted to a (const char *).

No, it is not. The type of a string literal in C is array of char,
not array of const char. When converted to a pointer, the pointer has
type pointer to char, not pointer to const char.

Attempting to modify a string literal in C is undefined behavior not
because the string literal is constant. It is undefined behavior
solely because the standard specifically states that it is.
 
J

Jack Klein

I've noticed that it's valid to pass a string literal (like "test") to a
function that expects a const char *. Does this mean that in C a string
literal is automatically converted to a const char * if I pass it to a
function?

TIA,
copx

As far as the cv-qualifiers (const and volatile) are concerned, the C
standard specifically allows you to pass a pointer to a less qualified
type to a function parameter of a more qualified type.

That is, you may pass a pointer to an ordinary object to a function
parameter of pointer to const object, pointer to volatile object, or
pointer to const volatile object.

The same is true of assignment.

What you cannot do without a cast is to go the other way.

So a pointer to a string literal gets converted to pointer to
modifiable char when passed to a function, and that is acceptable for
a parameter of pointer to const char.
 
O

Old Wolf

Jack Klein said:
(Old Wolf) wrote in comp.lang.c:
CBFalconer said:
I've noticed that it's valid to pass a string literal (like
"test") to a function that expects a const char *. Does this
mean that in C a string literal is automatically converted to
a const char * if I pass it to a function?

BTW, it isn't converted, that is what the string literal actually
is.

It is actually a (const char []), it is converted to a (const char *).

No, it is not. The type of a string literal in C is array of char,
not array of const char.

Sorry, I thought we were in c.l.c++ for some reason.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top