|
-
April 26th, 2007, 01:00 AM
#1
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
-
April 26th, 2007, 01:34 AM
#2
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";
Last edited by cilu; April 26th, 2007 at 06:31 AM.
-
April 26th, 2007, 01:36 AM
#3
Re: CStrings Question: What is the difference between char str[] and char* str
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
April 26th, 2007, 05:22 AM
#4
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.
-
April 26th, 2007, 05:42 AM
#5
Re: CStrings Question: What is the difference between char str[] and char* str
 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.).
-
April 26th, 2007, 06:44 AM
#6
Re: CStrings Question: What is the difference between char str[] and char* str
 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";
-
April 26th, 2007, 06:54 AM
#7
Re: CStrings Question: What is the difference between char str[] and char* str
 Originally Posted by Mitsukai
no it should be
Code:
char const* str2 ="For the perfecting of the saints";
What's the difference?
- petter
-
April 26th, 2007, 07:29 AM
#8
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
April 26th, 2007, 07:29 AM
#9
Re: CStrings Question: What is the difference between char str[] and char* str
Hi Mitsukai.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|