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

String Literal Question

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 memory holding the string
and the string might very well be held in read only memory.

However, I am sure that I read somewhere that the declaration char a[]
= "string literal", even though a is an array (and I understand the
differences between the two declarations), defines a such that it
might also be also held in read only memory and thus writing to
indexes of a might not work...

After some doubt about this popped into my mind, I have had a look
through the C faq and it seems to suggest that I am wrong - in the
second case one can use the array declared as above as normal, but I
would like to make sure.

Thanks,
Nick

Oct 8 '07 #1
5 2870
polas said:
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 memory holding the string
and the string might very well be held in read only memory.
Right. (Actually, we can say something stronger about p. It isn't merely
declared; it is actually defined, too.)

"string literal" is indeed a string literal, stored in (potentially)
read-only memory, and updating it invokes undefined behaviour (which means
it might work or it might do something obviously horrible, or it might do
something very subtle but gerharsterly). All this, you appear to know
already. So let's move on.
>
However, I am sure that I read somewhere that the declaration char a[]
= "string literal", even though a is an array (and I understand the
differences between the two declarations), defines a such that it
might also be also held in read only memory and thus writing to
indexes of a might not work...
No, the string literal is still a string literal, but your definition of
the a array reserves fifteen bytes of storage, and ***copies*** the string
literal (including the null terminator) into that storage, which is
writeable.
After some doubt about this popped into my mind, I have had a look
through the C faq and it seems to suggest that I am wrong - in the
second case one can use the array declared as above as normal, but I
would like to make sure.
You may rest assured that the array is writeable, yes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 8 '07 #2
On 8 Oct, 11:33, Richard Heathfield <r...@see.sig.invalidwrote:
polas said:
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 memory holding the string
and the string might very well be held in read only memory.

Right. (Actually, we can say something stronger about p. It isn't merely
declared; it is actually defined, too.)

"string literal" is indeed a string literal, stored in (potentially)
read-only memory, and updating it invokes undefined behaviour (which means
it might work or it might do something obviously horrible, or it might do
something very subtle but gerharsterly). All this, you appear to know
already. So let's move on.
However, I am sure that I read somewhere that the declaration char a[]
= "string literal", even though a is an array (and I understand the
differences between the two declarations), defines a such that it
might also be also held in read only memory and thus writing to
indexes of a might not work...

No, the string literal is still a string literal, but your definition of
the a array reserves fifteen bytes of storage, and ***copies*** the string
literal (including the null terminator) into that storage, which is
writeable.
After some doubt about this popped into my mind, I have had a look
through the C faq and it seems to suggest that I am wrong - in the
second case one can use the array declared as above as normal, but I
would like to make sure.

You may rest assured that the array is writeable, yes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Thanks very much for the reply and information - that clears it up for
me. The fact that the array actually copies the string literal makes
sense to me and clears up what I was misunderstanding

Nick

Oct 8 '07 #3
On Mon, 08 Oct 2007 10:33:31 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>polas said:
>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 memory holding the string
and the string might very well be held in read only memory.

Right. (Actually, we can say something stronger about p. It isn't merely
declared; it is actually defined, too.)

"string literal" is indeed a string literal, stored in (potentially)
read-only memory, and updating it invokes undefined behaviour (which means
it might work or it might do something obviously horrible, or it might do
something very subtle but gerharsterly). All this, you appear to know
already. So let's move on.
>>
However, I am sure that I read somewhere that the declaration char a[]
= "string literal", even though a is an array (and I understand the
differences between the two declarations), defines a such that it
might also be also held in read only memory and thus writing to
indexes of a might not work...

No, the string literal is still a string literal, but your definition of
the a array reserves fifteen bytes of storage, and ***copies*** the string
literal (including the null terminator) into that storage, which is
writeable.
It is probably an implementation detail as to whether the string
literal actually exists to be copied into the array or the compiler
uses another initialization technique to cause the array to contain
the specified value.
>
>After some doubt about this popped into my mind, I have had a look
through the C faq and it seems to suggest that I am wrong - in the
second case one can use the array declared as above as normal, but I
would like to make sure.

You may rest assured that the array is writeable, yes.

Remove del for email
Oct 10 '07 #4
Barry Schwarz said:

(in reference to: char arr[] = "initialiser string";)
It is probably an implementation detail as to whether the string
literal actually exists to be copied into the array or the compiler
uses another initialization technique to cause the array to contain
the specified value.
Yes. As far as I can see, a strictly conforming program would not be able
to tell the difference anyway.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 10 '07 #5
On Oct 8, 3:21 pm, polas <n...@helpforce.comwrote:
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 memory holding the string
and the string might very well be held in read only memory.

However, I am sure that I read somewhere that the declaration char a[]
= "string literal", even though a is an array (and I understand the
differences between the two declarations), defines a such that it
might also be also held in read only memory and thus writing to
indexes of a might not work...

After some doubt about this popped into my mind, I have had a look
through the C faq and it seems to suggest that I am wrong - in the
second case one can use the array declared as above as normal, but I
would like to make sure.
Example -
char *i_reg_fname = "none";

String constants are sequences of characters enclosed in double
quotes.
String literal is the formal term for a double-quoted string in C
source. 'i_reg_fname' is a pointer to characters.

Here,
Note that any attempt to modify the string that 'i_reg_fname' points
to will result in undefined behaviour.
that is, i_reg_fname[0]='q'; // not allowed . will cause undefined
behaviour. :(

Some compilers have a switch controlling whether string literals are
writable or not (for compiling old code),
and some may have options to cause string literals to be formally
treated as arrays of const char (for better error catching).

But irrespective of theat, if you have declared as below , then it is
possible to change.
char i_reg_fname[] = "none";
that is, i_reg_fname[0]='q'; // allowed . :)

i_reg_fname is a non-const pointer to char.
It may point to a string literal, but it isn't declared const.

An attempt to modify it will cause undefined behaviour .
Attempting to modify a string literal invokes undefined
behavior, because the C standard defines that attempting to modify a
string literal invokes undefined behavior.
It is because of the C standard and it is not 'const'.

In actual practice, the behaviour depends on where the compiler
decides to put its string constants. Some compilers have a switch
controlling whether
string literals are writable or not (for compiling old code),and some
may have options to
cause string literals to be formally treated as arrays of const char
(for better
error catching).

Earlier C didn not have the 'const' keyword, so if you wanted to
pass
a string literal to a particular function( In sucha a way that the
string will
not be modified inside the
function), then that particular function must take a 'char*'
argument.
Thats all.

Lot of information is available regarding this String Literal in
internet and groups. Also read the C-Faq (question 1.32 will give you
enough info).

Refer :- http://groups.google.co.in/group/com...3a3e04/?hl=en#

Karthik Balaguru

Oct 11 '07 #6

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 *...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
9
by: copx | last post by:
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 *...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
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...
19
by: Jonathan Shan | last post by:
Hello everyone, What is this string? {name = "172.16.0.240\000\000\000"} name was declared using this: char name; Why do I count more than 16 elements?
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";
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
9
by: somenath | last post by:
Hi All, I was going through one piece of code which is written by an experience programmer and it is working fine. But I think the use of "strstr" is not proper because it may show undefined...
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: 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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.