1,105,633 Community Members

pointer to char array

Member Avatar
harish9
Newbie Poster
4 posts since Mar 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

*ptr = 'hello world";

if we try to modify the content the result is undefined when i worked with gcc in unix.
But i can modify the content in Turbo c. How it worked i got confussed.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

When you have a pointer defined like so

char *ptr = "hello world";

It creates a read only character pointer. If you require a char array that's writable then use.

char ptr[] = "hello world";
Member Avatar
rubberman
Senior Poster
3,999 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
1
 

This is one of those things that the standards allow implementations to "do their own thing" on. A string literal may (or may not) be in read-only memory as a constant. So, gerard4143 is absolutely correct if you want to modify the contents of the string in a platform-neutral manner. Neither gcc nor Turbo are breaking the rules. It's just that the rules are flexible in such cases. Caveat Programmer! :-)

Member Avatar
harish9
Newbie Poster
4 posts since Mar 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanku for ur information. I know it is stored in read only memory and i also know that we can't alter a value but how it worked in Turbo C thats my question.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

thanku for ur information. I know it is stored in read only memory and i also know that we can't alter a value but how it worked in Turbo C thats my question.

Why it worked in Turbo C, read Rubberman's post.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

if we try to modify the content the result is undefined when i worked with gcc in unix.

Correction: the result is always undefined, regardless of compiler. Whether it "works" or not depends on how the compiler implements string literals and enforces modification. Even if it "works", you could still be creating a subtle error down the line.

The lesson to be learned is don't rely on undefined behavior.

Member Avatar
quynyth
Newbie Poster
1 post since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am not familiar this topic of C language but I have backgrounds on some basic from the language. I hope you can help me with this topic.

basic math test

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

I am not familiar this topic of C language but I have backgrounds on some basic from the language. I hope you can help me with this topic.

basic math test

What is this?

Member Avatar
rubberman
Senior Poster
3,999 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
0
 

What is this?

Trolling I think.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article