473,394 Members | 1,875 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,394 software developers and data experts.

string literals question

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
Nov 14 '05 #1
9 4859

"copx" <in*****@invalid.com> wrote in message
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.


Nov 14 '05 #2
copx wrote:

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.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02

Nov 14 '05 #3
In article <news:41**********************@newsread2.arcor-online.net>
copx <in*****@invalid.com> wrote:
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
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'.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #5
CBFalconer wrote:
copx wrote:
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.


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

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #6
CBFalconer <cb********@yahoo.com> wrote:

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 *).
Nov 14 '05 #7
On 5 Sep 2004 17:15:17 -0700, ol*****@inspire.net.nz (Old Wolf) wrote
in comp.lang.c:
CBFalconer <cb********@yahoo.com> wrote:

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. 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
On Sun, 5 Sep 2004 18:51:31 +0200, "copx" <in*****@invalid.com> wrote
in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
Jack Klein <ja*******@spamcop.net> wrote:
(Old Wolf) wrote in comp.lang.c:
CBFalconer <cb********@yahoo.com> wrote:
>
> 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.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
52
by: junky_fellow | last post by:
char *str1 = "Hello"; char arr1 = { "Hello" }; char arr2 = { 'H', 'e', 'l', 'l', 'o' }; Is it legal to modify str1, arr1 and arr2 ?
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
5
by: news.chi.sbcglobal.net | last post by:
I have a question about string literals with C++/CLI. Is there a difference between the following two lines? String ^s1 = gcnew String("Hello"); String ^s2 = gcnew String(L"Hello"); I have...
8
by: gthorpe | last post by:
Hi, I have a question about string constants. I compile the following program: #include <stdio.h> #include <string.h> int main(void) { char str1 = "\007";
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
15
by: s0suk3 | last post by:
Hi, I've heard that a string literal has type 'char *' (i.e., a pointer to a char object). But I'm confused as to why this works: char *GetString(void) { return "hello"; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.