char * query [modified]
-
I've small piece of code,My query is , why line in bold not work ?
int main(int argc, char* argv[])
{
char *pp=new char[2];
char c='I';
char *str="test";
strcpy(pp,"dynamic");
pp[0]=c;//this works
cout< str[0]=c; //why this doesn't work ?
delete []pp;
return 0;
}-- modified at 6:56 Tuesday 12th September, 2006
Prasad Notifier using ATL
-
I've small piece of code,My query is , why line in bold not work ?
int main(int argc, char* argv[])
{
char *pp=new char[2];
char c='I';
char *str="test";
strcpy(pp,"dynamic");
pp[0]=c;//this works
cout< str[0]=c; //why this doesn't work ?
delete []pp;
return 0;
}-- modified at 6:56 Tuesday 12th September, 2006
Prasad Notifier using ATL
Because you do not know where the compiler placed the string "test". What you did with that one was telling the compiler to give you the adress of "test". That means, the compiler has to find a location to place "test" in. And that might, following the C++-Standard, be anywhere, even locations that are not writeable from inside the code. You should not make any assumptions on where the compiler places this. You need to assign memory to the char*, then copy "test" into the char*, in order for this to work.
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
-
Because you do not know where the compiler placed the string "test". What you did with that one was telling the compiler to give you the adress of "test". That means, the compiler has to find a location to place "test" in. And that might, following the C++-Standard, be anywhere, even locations that are not writeable from inside the code. You should not make any assumptions on where the compiler places this. You need to assign memory to the char*, then copy "test" into the char*, in order for this to work.
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
Ok.Thanks. I was in assumption that,
char* str="test";
cout<
is possible , then reverse is also possible.
Thanks.Prasad
Notifier using ATL -
I've small piece of code,My query is , why line in bold not work ?
int main(int argc, char* argv[])
{
char *pp=new char[2];
char c='I';
char *str="test";
strcpy(pp,"dynamic");
pp[0]=c;//this works
cout< str[0]=c; //why this doesn't work ?
delete []pp;
return 0;
}-- modified at 6:56 Tuesday 12th September, 2006
Prasad Notifier using ATL
Because *str is a pointer to a string constant "test", and not an array of characters. The pointer *str can be made to point to a different location, but the contents of a string constant cannot be changed (the ANSI C Standard actually says trying to modify the contents of string constant is undefined - ie. sometimes it might work, other times it may not - so you should never be writing code like this) Writing the following would work because str would be an array of characters. The contents of the array could be changed, but str will always refer to the same storage. chat str[] = "test"; str[0] = c;
-
I've small piece of code,My query is , why line in bold not work ?
int main(int argc, char* argv[])
{
char *pp=new char[2];
char c='I';
char *str="test";
strcpy(pp,"dynamic");
pp[0]=c;//this works
cout< str[0]=c; //why this doesn't work ?
delete []pp;
return 0;
}-- modified at 6:56 Tuesday 12th September, 2006
Prasad Notifier using ATL
prasad_som wrote:
char *str="test";
prasad_som wrote:
I've small piece of code,My query is , why line in bold not work ?
Because compiler places these type of strings in read only memory area.
Nibu thomas A Developer Programming tips[^] My site[^]
-
Because *str is a pointer to a string constant "test", and not an array of characters. The pointer *str can be made to point to a different location, but the contents of a string constant cannot be changed (the ANSI C Standard actually says trying to modify the contents of string constant is undefined - ie. sometimes it might work, other times it may not - so you should never be writing code like this) Writing the following would work because str would be an array of characters. The contents of the array could be changed, but str will always refer to the same storage. chat str[] = "test"; str[0] = c;
Thanks for informatioin.
Prasad Notifier using ATL
-
prasad_som wrote:
char *str="test";
prasad_som wrote:
I've small piece of code,My query is , why line in bold not work ?
Because compiler places these type of strings in read only memory area.
Nibu thomas A Developer Programming tips[^] My site[^]
thanks.
Prasad Notifier using ATL
-
I've small piece of code,My query is , why line in bold not work ?
int main(int argc, char* argv[])
{
char *pp=new char[2];
char c='I';
char *str="test";
strcpy(pp,"dynamic");
pp[0]=c;//this works
cout< str[0]=c; //why this doesn't work ?
delete []pp;
return 0;
}-- modified at 6:56 Tuesday 12th September, 2006
Prasad Notifier using ATL
prasad_som wrote:
char *pp=new char[2]; ... strcpy(pp,"dynamic");
Slightly off topic ... the above code will corrupt memory. pp is a pointer to a character array that has allocated space for 2 characters. "dynamic" is longer than 2, so copying it into pp will overwrite memory you don't control.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
prasad_som wrote:
char *pp=new char[2]; ... strcpy(pp,"dynamic");
Slightly off topic ... the above code will corrupt memory. pp is a pointer to a character array that has allocated space for 2 characters. "dynamic" is longer than 2, so copying it into pp will overwrite memory you don't control.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
thanks, for correcting me. I overlooked it, to say my point.:)
Prasad Notifier using ATL