Click to See Complete Forum and Search --> : CStrings Question: What is the difference between char str[] and char* str


stephenprogrammer07
April 26th, 2007, 01:00 AM
Hi;

I guess I'm a newbie with this question.

My question is about C strings.


I'm trying to figure out why the behavior of these 2 strings are different as it relates to toupper() function ?

char str1[] ="For the perfecting of the saints";
char* str2 ="For the perfecting of the saints";


//when I call ucase(str1) everything works perfectly.

ucase(str1);

//but when i call

ucase(str2)

//it crashes. Can you tell me what's the difference between char str1[] and char* str2?



Here's the ucase() function i'm calling below.

//convert to uppercase
void ucase(char* buffer)
{
int len = strlen(buffer);

for(int i = 0; i < len; i++)
{
buffer[i] = toupper(buffer[i]);
}
}




I can loop through both just fine using something like this
for(int i =0; i<36; i++){
printf("here %c\n", str1[i]);
}

but when it seems to me toupper breaks when you use it with char* x for some reason.

Thanks in Advance.

Stev

cilu
April 26th, 2007, 01:34 AM
char[] is an array of char.
char str1[] ="For the perfecting of the saints";
str1 is an array of char initialized with a string literal.

char* is a pointer to char
char* str2 ="For the perfecting of the saints";
str2 is a pointer to char holding the address of the first char of the string literal (which is immutable, i.e. you cannot change; attempting to do that is yields undefined behaviour). Hence it should rather be:
const char* str2 ="For the perfecting of the saints";

Hobson
April 26th, 2007, 01:36 AM
http://www.codeguru.com/forum/showthread.php?t=420034&highlight=string+literal
http://www.codeguru.com/forum/showthread.php?t=417342&highlight=string+literal

Everytime you try modifying a string literal, God kills a kitten! And your program as well...

Cheers

davide++
April 26th, 2007, 05:22 AM
Hi all.

In addition what Cilu said, char[] defines an array of char initialized with a string literal and closed by the '\0', the special character that ends the strings;
When you call ucase with char[], the function strlen finds the '\0' and all works fine; instead, when you call ucase with char* strlen don't find the '\0' so your program crashes.

Philip Nicoletti
April 26th, 2007, 05:42 AM
When you call ucase with char[], the function strlen finds the '\0' and all works fine; instead, when you call ucase with char* strlen don't find the '\0' so your program crashes.

strlen works fine with char* variables as given in the post.

The problem is that ucase attempts to modify a string literal.
This results in undefined behavior. It might do what you want,
it might not. (For example, ucase would not crash with
VC++ version 5, but will for versions after that.).

Mitsukai
April 26th, 2007, 06:44 AM
Hence it should rather be:
const char* str2 ="For the perfecting of the saints";

no it should be
char const* str2 ="For the perfecting of the saints";

just try compiling this:


const char* str1 ="For the perfecting of the saints";
char const* str2 ="For the perfecting of the saints";
str2 = "nooooooooooooot";
str1 = "nooooooooooooot";

wildfrog
April 26th, 2007, 06:54 AM
no it should be


char const* str2 ="For the perfecting of the saints";


What's the difference?

- petter

TheCPUWizard
April 26th, 2007, 07:29 AM
"const" only matters chich side of the "*" it is on. To the left (either before or after the type), the DATA is const. To the right of the "*", the pointer is const (must always point to the same location), but does not imply that the data is const.

ltcmelo
April 26th, 2007, 07:29 AM
Hi Mitsukai.

no it should be
char const* str2 ="For the perfecting of the saints";


Both char const* and const char* should mean the same thing. What compiler are you using? The important thing is that the asterisc is to the right of the const keyword.