Archived

This topic is now archived and is closed to further replies.

strings

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

right, I admit it. Strings are confusing me big time. I''ll start with a basic question I probably should have asked a long time ago: Whats the difference between char * s="xxxxx"; and char s[]="xxxxx"; in terms of memory? I''d really appreciate a good explaination of this. I probably should stop, My doctor says I have the wrists of an 80-year old

Share this post


Link to post
Share on other sites
the first initializes a pointer to point to a constant string somewhere in the string table. You cannot modify the string that is pointed to.

The second initializes a character array of the appropriate size to hold the given string, and fills it with the string. It acts like any other array, and you can modify it.

BTW, this is really more appropriate to the For Beginners forum.

"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on November 13, 2003 4:15:00 PM]

Share this post


Link to post
Share on other sites
here''s some more data that might be more than you need, but might help solidify your understanding ...


char *s1; // declares a pointer to a char, pointer not initialized
// uses 1 pointer of stack memory (4 bytes on 32 bit platform)
char *s2 = "bobby"; // declares a pointer to a char that
initialy points to the constant string "bobby"
// uses 1 pointer of stack memory, and allocates 6 bytes of data in the data section (if these letters are not already in the data section, in which case the compiler can share)

char s3[6]; // declares an array of 5 chars, values not initialized;
// uses 6 characters of stack memory, the fact that s refers to the address of the first char is implicit and need not cause a pointer to be allocated on the stack (because it cannot be assigned to)
char s4[6] = "bobby" // declares an array of 6 chars, who''s values are set to ''b'' ''o'' ''b'' ''b'' ''y'' ''\0''
// save as above ... but these values are initialized instead of leftover garbage.

// in C and C++ arrays and pointers are identical in how they can be used, except ONE THING - you cannot change the address the array refers to ...
s1 = s2; // valid - s1 now points to same memory as s2
s1 = s4; // valid - s1 now points to same memory as array s4
s3 = s1; // INVALID - s3 cannot be set, only it''s VALUES may be set
s3 = s4; // INVALID - same reason

// notice all of the above only try to change pointers ... to change content, something like this must be done ...
strcpy(s3, s4); // valid, s4 would now also hold the letters ''b'' ''o'' ''b'' ''b'' ''y'' ''\0''
strcpy(s3, s2); // same as above
strcpy(s3, s1); // INVALID, because s1 is NOT a null terminated string, so strcpy should not be called with it as a source
strcpy(s1, s4); // INVALID, because s1 does not have any allocated memory, and therefore cannot hold the string "bobby"

// for the last choice you would do this
s4 = new char[strlen(s1) + 1];
strcpy(s1, s4);
// don''t forget to delete[] s4 when done with it


best of luck to you

Share this post


Link to post
Share on other sites