Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer to constant char

Status
Not open for further replies.

anxious7

Technical User
Nov 23, 2007
7
why this is not working ?

Code:
char* ptr= "Hello";
*ptr='s';
printf("%s\n",ptr);
[\code]

and this does

[code]
char a[] = "Hello";
char* ptr= &a;
*ptr='s';
printf("%s\n",ptr);
[\code]
 
Closer, but it's / not \ in the code tags.

> why this is not working ?
Because a "string" is stored in read-only memory. Being read-only, the OS will kill your program as soon as you try to modify it.

> and this does
Because what you have now is an initialised char array, which you can change all you want.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top