Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2008, 1:18 PM   #11
Ka0s
Newbie
 
Join Date: Apr 2008
Posts: 2
Rep Power: 0 Ka0s is on a distinguished road
Re: char[] vs char*

Well... you are saying that it's not possible to change a string literal declared has a pointer but i've compiled this code and it worked:

#include <stdio.h>

int main() {
	char *a = "hello";
	printf("%s\n", a);
	*a = 'a';
	printf("%s\n", a);

	return 0;
}

When i run the program it shows "hello" followed by "aello".
Can you tell me why can i modify the string pointed by "char *a"?
Ka0s is offline   Reply With Quote
Old Apr 15th, 2008, 4:21 PM   #12
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 927
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: char[] vs char*

Quote:
Originally Posted by Ka0s
When i run the program it shows "hello" followed by "aello".
Can you tell me why can i modify the string pointed by "char *a"?
My understanding (though you might want to wait until grumpy or another person more knowledgeable than I weighs in on the subject) is that your program is exhibiting 'undefined behavior'. The reason for the immutable nature of string literals is mainly to reduce redundancy, making the executable smaller. Take the following code:
C Syntax (Toggle Plain Text)
  1. char *a = "hello", *b = "hello";
A compiler can see that both are being initialized with identical string literals, and can thus store a single copy of the contents, and point both pointers at it. Allowing modification of the contents could thus have unintended consequences.

Some systems will actually store the string in memory flagged as read-only, and a write access will raise a hardware exception, causing your program to crash. Others will not detect the access, but just because an illegal operation is not trapped, that does not make it okay. Remember that 'undefined' means your program may take any action, including acting in a manner you think is 'correct'.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Apr 15th, 2008, 5:38 PM   #13
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: char[] vs char*

And in support of lectricpharaoh's post: your code crashed, after printing the first line, when I attempted to run it.
Sane is offline   Reply With Quote
Old Apr 16th, 2008, 5:21 AM   #14
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Re: char[] vs char*

The short answer, as electricpharoah said, is undefined behaviour.

For discussion another day, there are a few historic reasons the code will compile, although the runtime behaviour is not defined. This is due to very old (well before the first C standard) anomalies in the C language related to the relationship between string literals, arrays of char, and pointers to char, from days when the language did not support the const keyword.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
25 ways to efficiently sort an array/list of integers Jimbo Software Design and Algorithms 36 Mar 3rd, 2008 6:31 AM
500 Ways to Program Numbers 1 Through 10! Sane Coder's Corner Lounge 637 Jan 5th, 2008 10:15 PM
char * to char[] titaniumdecoy C++ 6 Jul 28th, 2006 12:53 PM
char[] problem Kilo C++ 13 Jun 19th, 2006 2:18 AM
Convert char[] to int King C++ 18 Jan 31st, 2006 3:55 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC