char array versus pointer to string constant question

Member
Posts: 321
Joined: 2004.10
Post: #1
In Kernighan and Ritchie's "The C Programming Language", page 104
talks about differencies between these two definitions (I've changed the contents of pmessage to help the discussion):

char amessage[] = "now is the time"; /* an array */
char *pmessage = "for all good men"; /* a pointer */

It goes on to say: "amessage is an array, just big enough to hold the sequence of characters and '\0' that initializes it. Individual characters within the array may be changed... On the other hand, pmessage is a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents."

Two things bother me in this paragraph about the pmessage pointer:

1) it says pmessage points to a "string constant". Why is this a constant? I see
the string "for all good men", but why is it constant? For this to be true, shouldn't something like this be written:

const char *pmessage = "for all good men";

2) Also, it says the "result is undefined if you try to modify the string contents".
I created a test program and sure enough the program abended.

Quote:char *pmessage = "for all good men";
char *p = pmessage;
while ( *p != '\0')
{
*p = 'X'; /* program aborts here */
p++;
}

But why? Isn't the string "for all good men" somewhere in memory. Then we have a pointer point to this location. And then use it to modify the contents.
Isn't that what memory and pointers do? I just don't see why this is a problem.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
In order to understand this, you have to understand how variables are kept. Variables declared globally are stored inside the text section of your program. Essentially, they're archived on the disk, and loaded into the memory at launch. Most of that memory is mutable, so in the case of the array "amessage", you can change its contents. String constants are also essentially an array stored in the text section. However, there is only 1 copy of each string constant. Basically, if you put in "hello" somewhere in your program, then again use the constant "hello" elsewhere, it will use the same pointer. As such, the memory that the string constants are used in is often designated as read only, otherwise if you changed that single pointer, it will change everywhere else. Therefore, any time you try to modify a string constant that resides in this special block of memory, your program will abort.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #3
I never knew this and I've read lots of C/C++ books. In fact, can you
give me a reference to where you learned this? Whatever you're reading,
thats what I want Smile
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
Actually, most of what I know about the internal workings of the nitty gritty I learned from a class. Rasp (more specifically, the class on assembly) If I find something that I don't know, I also tend to go to Google. I don't really have any books on C, though. I do have The C++ Programming Language, though, which may or may not have information on this. (I'm not sure)
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #5
WhatMeWorry Wrote:In Kernighan and Ritchie's "The C Programming Language", page 104
talks about differencies between these two definitions (I've changed the contents of pmessage to help the discussion):

char amessage[] = "now is the time"; /* an array */
char *pmessage = "for all good men"; /* a pointer */
Um, I thought in C you must put the in the length in the array, and the ability to make it "just big enough" by leaving it blank ( [] ) was added in C++?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
Nope, that's not specific to C++.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #7
You don't have to specify the size if it's an extern declaration or if the size can be determined by the initializer.
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #8
Ahh, thanks Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  formatting value in a string sefiroths 3 4,140 Dec 12, 2011 02:41 AM
Last Post: sefiroths
  Getting a value from a String Megamac04 5 3,919 Jan 31, 2010 04:23 PM
Last Post: smasher
  C: Global Variables versus Parameters Lizard Man 10 6,446 Jan 13, 2010 08:22 PM
Last Post: Lizard Man
  "Initializer element is not constant" for NSNumbers Coyote 5 6,061 Oct 22, 2009 09:37 AM
Last Post: OneSadCookie
  Parsing from a string to something faster? Madrayken 3 3,202 Aug 10, 2009 03:32 PM
Last Post: smasher