Something I never learned?
Would somebody please tell me why the code below works, but the code below that gives an illegal access message.
Code:
char MyString[] = "465.9900";
MyString[0] = '0';
Code:
char* MyString = "465.9900";
MyString[0] = '0';
Doesn't the pointer method do the same thing?
Re: Something I never learned?
Code:
char* MyString = "465.9900";
MyString[0] = '0';
You can assign any array size to mystring however it hasn't been
done yet, so if you add that it should work.
Code:
char* MyString = "465.9900";
MyString=new char[10];
MyString[0] = '0';
Re: Something I never learned?
Quote:
Originally posted by CodeMonkey
Would somebody please tell me why the code below works, but the code below that gives an illegal access message.
Code:
char MyString[] = "465.9900";
MyString[0] = '0';
Code:
char* MyString = "465.9900";
MyString[0] = '0';
Doesn't the pointer method do the same thing?
The first one creates an array of sufficient size and copies that string into it. The array is modifyable.
The second one is just a pointer to that string which resides somewhere in the memory. Trying to write to it will result in an access violation error.
Re: Re: Something I never learned?
Quote:
Originally posted by Travis Dane
Code:
char* MyString = "465.9900";
MyString=new char[10];
MyString[0] = '0';
That will not keep the old data since the pointer is repointed.
Re: Something I never learned?
They are 2 very different things
Code:
char MyString[] = "465.9900";
MyString[0] = '0';
This means allocate space on the stack and copy "465.9900" to it. The compiler sees "465.9900" as a pointer to constant data and copies it byte for byte to memory on the stack that you can alter.
Code:
char* MyString = "465.9900";
MyString[0] = '0';
Here you are simply copying the pointer to the cont data......when you try change the array, you trying to change const data and the OS will likely raise an exception.
<edit>Magos was too quick for me</edit>