Click here to Skip to main content
15,889,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I came across the following condition . It may be a simplest solution for you but i will appreciate if you help me in this regard.


char *p="hello";
*(p+1) = 'A';



This program gives Access violation error for second line.

It gives the same error even if i use

p[1]='A';
Posted
Updated 4-Jul-11 20:23pm
v2

char *p = "hello";
char pArr[] = "Hello";


The above two are not the same. When you declare it as a pointer and initialize it with a value, the compiler puts it in the Data Segment[^], AND in the "Initialised and read only" area. When you try to change contents stored in this section, it should blow up (that's what you're experiencing).

However, the latter one is just an array (the contents are stored in the code executing thread's stack) and therefore the contents can be changed as you wish.
 
Share this answer
 
v2
Comments
Espen Harlinn 9-Jul-11 20:27pm    
Right, my 5 :)
Change string declaration to char p[]="hello";. This should allow you to access p elements by index; and you can always turn it into char* by taking first character address
 
Share this answer
 
v2
Comments
Christian Graus 5-Jul-11 2:42am    
Interesting, but, is it correct behaviour ? The VS debugger can find and set the values just fine. Would it blow up in a release build, or just in debug ?
Timberbird 5-Jul-11 2:58am    
Yes, in release version it can - and I think it will - cause an error. See my comment below
PrafullaVedante 5-Jul-11 2:43am    
It works. :)

But why the earlier case fails ? :(
Timberbird 5-Jul-11 2:57am    
For example, see this. Strings declared like char* p="Smth"; should not be modified - consider them constants. The problem is that the compiler can place your string in read-only memory, and I guess that's what happens in release version :)
Stefan_Lang 5-Jul-11 6:08am    
The problem is that string literals are technically constants, and the compiler is allowed to place them in read-only memory. Therefore, at runtime, trying to modify that memory will lead to undefined behaviour!

The suggestion works because it creates an array on the stack, rather than just a pointer into read-only memory.
Why would you do this ? It looks like a circus trick to me, not a programming problem. You should access the variables inside p by index, not like this.
 
Share this answer
 
Comments
PrafullaVedante 5-Jul-11 2:22am    
It gives the same error for p[1]='A' also.
Christian Graus 5-Jul-11 2:26am    
Interesting, it works in the debug window. I don't know why it does that, I would not have expected it to.
Sergey Alexandrovich Kryukov 5-Jul-11 3:31am    
This is a known sloppiness of Microsoft -- there was the discussion. It should throw exception in all cases.
--SA
PrafullaVedante 5-Jul-11 2:32am    
:(
Mohibur Rashid 10-Jul-11 1:45am    
I do not agree with Christian Graus at all

here is an example of using Rajesh R Subramanian way

a simple strcpy function

void strcpy(char *dest, char *src)
{
while((*src)!=0){(*dest)=(*src);src++;}
}


and another solution


void strcpy(char *dest, char *src)
{
for(int i;dest[i]!=0;i++)
{
src[i]=dest[i];
}
}


which one is better?

if you know how to handle pointer you dont need to define another pointless variable to track down your index
The string "hello" is stored in a read-only area of the memory. It is a constant string value.
 
Share this answer
 
v2
As a sidenote to the answer by Rajesh:
Put something like this in a dll and you're able to share data between different instance of same dll.
#pragma data_seg (".MYSEG")
    int g_nInteger = 0;
    char g_szString[] = "hello world";
#pragma data_seg()
// Tell the linker that the section
// .MYSEG is readable, writable and is shared.
#pragma comment(linker, "/SECTION:.MYSEG,RWS")


Sometimes it's quite useful :)

Best regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900