CStrings Question: What is the difference between char str[] and char* str
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
Re: CStrings Question: What is the difference between char str[] and char* str
char[] is an array of char.
Code:
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
Code:
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:
Code:
const char* str2 ="For the perfecting of the saints";
Re: CStrings Question: What is the difference between char str[] and char* str
Re: CStrings Question: What is the difference between char str[] and char* str
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.
Re: CStrings Question: What is the difference between char str[] and char* str
Quote:
Originally Posted by davide++
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.).
Re: CStrings Question: What is the difference between char str[] and char* str
Quote:
Originally Posted by cilu
Hence it should rather be:
Code:
const char* str2 ="For the perfecting of the saints";
no it should be
Code:
char const* str2 ="For the perfecting of the saints";
just try compiling this:
Code:
const char* str1 ="For the perfecting of the saints";
char const* str2 ="For the perfecting of the saints";
str2 = "nooooooooooooot";
str1 = "nooooooooooooot";
Re: CStrings Question: What is the difference between char str[] and char* str
Quote:
Originally Posted by Mitsukai
no it should be
Code:
char const* str2 ="For the perfecting of the saints";
What's the difference?
- petter
Re: CStrings Question: What is the difference between char str[] and char* str
"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.
Re: CStrings Question: What is the difference between char str[] and char* str
Hi Mitsukai.
Quote:
Originally Posted by Mitsukai
no it should be
Code:
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.