strcpy dilemma - it works half way
-
I am going crazy over this simple test code.
char *Result ={"Result init "};
char *Source = {"123"};
char *Destination = {"456"};I can verify that all strings are as initialized. Now I run this strcpy
Result = strcpy(Source,Destination);
Now I can verify, using same approach as before , that
Result = 123
- AKA orignal Source, it wasResult init
before strcpy was run. and Source is STILL123
. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA456
What am I doing wrong? strcpy executes giving correct return, but unmodified Destination. Am I doing the initialization correctly? Am I nuts? Standard stdio using GCC compiler /tools on Arduino IDE. Thank for your help. Vaclav -
I am going crazy over this simple test code.
char *Result ={"Result init "};
char *Source = {"123"};
char *Destination = {"456"};I can verify that all strings are as initialized. Now I run this strcpy
Result = strcpy(Source,Destination);
Now I can verify, using same approach as before , that
Result = 123
- AKA orignal Source, it wasResult init
before strcpy was run. and Source is STILL123
. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA456
What am I doing wrong? strcpy executes giving correct return, but unmodified Destination. Am I doing the initialization correctly? Am I nuts? Standard stdio using GCC compiler /tools on Arduino IDE. Thank for your help. VaclavI think you have the parameter names backward so it's confusing to me. After the strcpy, Result and Source should contain the same address, the value there should be 465, and the value at Destination should also be 456. Is that what you see? Is that not what you want? Be aware that you are not implementing a "swap" at all. Unless there are other concerns, if you want to "swap" then swap the pointers, not the values:
Result = Destination;
Destination = Source;
Source = Result; -
I am going crazy over this simple test code.
char *Result ={"Result init "};
char *Source = {"123"};
char *Destination = {"456"};I can verify that all strings are as initialized. Now I run this strcpy
Result = strcpy(Source,Destination);
Now I can verify, using same approach as before , that
Result = 123
- AKA orignal Source, it wasResult init
before strcpy was run. and Source is STILL123
. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA456
What am I doing wrong? strcpy executes giving correct return, but unmodified Destination. Am I doing the initialization correctly? Am I nuts? Standard stdio using GCC compiler /tools on Arduino IDE. Thank for your help. VaclavIt should not work at all. String constants declared as above are read-only, so you should not be able to use
strcpy
to overwrite one of them by another. When I try it I get an access violation on the write, as I would expect. I am not sure why your system does not give the fault, maybe a peculiarity of Arduino. But sincestrcpy
is supposed to return the pointer to the destination field,Result
will now point to the data thatSource
points to, i.e. "123", but the original data should be untouched. -
I am going crazy over this simple test code.
char *Result ={"Result init "};
char *Source = {"123"};
char *Destination = {"456"};I can verify that all strings are as initialized. Now I run this strcpy
Result = strcpy(Source,Destination);
Now I can verify, using same approach as before , that
Result = 123
- AKA orignal Source, it wasResult init
before strcpy was run. and Source is STILL123
. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA456
What am I doing wrong? strcpy executes giving correct return, but unmodified Destination. Am I doing the initialization correctly? Am I nuts? Standard stdio using GCC compiler /tools on Arduino IDE. Thank for your help. VaclavYou cannot do that, it is a mistake. Your variables point to statically allocated (i.e. constant) strings you must NOT try to change. The following program
#include <stdio.h>
#include <string.h>int main()
{
char Result[] ="Result init ";
char Source[] = "123";
char Destination[] = "456";strcpy(Destination, Source);
printf("%s %s\n", Destination, Source);
return 0;
}on the other hand, outputs
123 123
Please note, parameter order in
strcpy
is important (like in any otherC
function, after all). -
You cannot do that, it is a mistake. Your variables point to statically allocated (i.e. constant) strings you must NOT try to change. The following program
#include <stdio.h>
#include <string.h>int main()
{
char Result[] ="Result init ";
char Source[] = "123";
char Destination[] = "456";strcpy(Destination, Source);
printf("%s %s\n", Destination, Source);
return 0;
}on the other hand, outputs
123 123
Please note, parameter order in
strcpy
is important (like in any otherC
function, after all).Thanks, one of these days I'll hope to learn. I did change the initialization and it did the trick with the exception of Result. Can you explain to me why Result needs to be declared this way? I appreciate all you guys time and I am sorry about the poor choice of variable names I did. Thanks. I am enclosing my full test code, just FYI , so do not pay much attention to it. char *Result = {"0007890"}; char Destination[] = {"456"}; char Temp[] = {" "}; char Source[] = {"123"}; #ifdef DEBUG lcd_i2c.clear(); //lcd_i2c.print("BuildAttribute..."); lcd_i2c.setCursor(0, 0); lcd_i2c.print("Result "); lcd_i2c.print(Result ); //for(;;); lcd_i2c.setCursor(0, 1); lcd_i2c.print("Source " ); lcd_i2c.print(Source ); //strcpy(PassedAttribute, "attribute"); lcd_i2c.setCursor(0, 2); lcd_i2c.print("Dest " ); lcd_i2c.print(Destination ); lcd_i2c.setCursor(0, 3); //lcd_i2c.print(Attribute); //delay(2000); //for(;;); //return PassedAttribute; #endif //Result = strncpy(Source, "TESTDestination",2 ); printf("%s %s\n", Destination, Source); Result = strcpy(Destination, Source); printf("%s %s\n", Destination, Source); //for (;;); #ifdef DEBUG lcd_i2c.clear(); //lcd_i2c.print("BuildAttribute..."); lcd_i2c.setCursor(0, 0); lcd_i2c.print("Result "); lcd_i2c.print(Result ); //for(;;); lcd_i2c.setCursor(0, 1); lcd_i2c.print("Source " ); lcd_i2c.print(Source ); //strcpy(PassedAttribute, "attribute"); lcd_i2c.setCursor(0, 2); lcd_i2c.print("Dest " ); lcd_i2c.print(Destination ); lcd_i2c.setCursor(0, 3); lcd_i2c.print("Temp "); lcd_i2c.print(Temp); //delay(2000); for (;;); //return PassedAttribute; #endif for(;;);
-
Thanks, one of these days I'll hope to learn. I did change the initialization and it did the trick with the exception of Result. Can you explain to me why Result needs to be declared this way? I appreciate all you guys time and I am sorry about the poor choice of variable names I did. Thanks. I am enclosing my full test code, just FYI , so do not pay much attention to it. char *Result = {"0007890"}; char Destination[] = {"456"}; char Temp[] = {" "}; char Source[] = {"123"}; #ifdef DEBUG lcd_i2c.clear(); //lcd_i2c.print("BuildAttribute..."); lcd_i2c.setCursor(0, 0); lcd_i2c.print("Result "); lcd_i2c.print(Result ); //for(;;); lcd_i2c.setCursor(0, 1); lcd_i2c.print("Source " ); lcd_i2c.print(Source ); //strcpy(PassedAttribute, "attribute"); lcd_i2c.setCursor(0, 2); lcd_i2c.print("Dest " ); lcd_i2c.print(Destination ); lcd_i2c.setCursor(0, 3); //lcd_i2c.print(Attribute); //delay(2000); //for(;;); //return PassedAttribute; #endif //Result = strncpy(Source, "TESTDestination",2 ); printf("%s %s\n", Destination, Source); Result = strcpy(Destination, Source); printf("%s %s\n", Destination, Source); //for (;;); #ifdef DEBUG lcd_i2c.clear(); //lcd_i2c.print("BuildAttribute..."); lcd_i2c.setCursor(0, 0); lcd_i2c.print("Result "); lcd_i2c.print(Result ); //for(;;); lcd_i2c.setCursor(0, 1); lcd_i2c.print("Source " ); lcd_i2c.print(Source ); //strcpy(PassedAttribute, "attribute"); lcd_i2c.setCursor(0, 2); lcd_i2c.print("Dest " ); lcd_i2c.print(Destination ); lcd_i2c.setCursor(0, 3); lcd_i2c.print("Temp "); lcd_i2c.print(Temp); //delay(2000); for (;;); //return PassedAttribute; #endif for(;;);
-
Result
must be declared as pointer to character (char *
) because (as already explained by Richard) you are assigning it withstrcpy
return value. Please note such an assignment is useless sincestrcpy
result is already inDestination
array. -
I am going crazy over this simple test code.
char *Result ={"Result init "};
char *Source = {"123"};
char *Destination = {"456"};I can verify that all strings are as initialized. Now I run this strcpy
Result = strcpy(Source,Destination);
Now I can verify, using same approach as before , that
Result = 123
- AKA orignal Source, it wasResult init
before strcpy was run. and Source is STILL123
. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA456
What am I doing wrong? strcpy executes giving correct return, but unmodified Destination. Am I doing the initialization correctly? Am I nuts? Standard stdio using GCC compiler /tools on Arduino IDE. Thank for your help. VaclavThis should exactly work:
#include #include int main(void)
{
char *Result = "Result init ";
char Source[256] = "123";
char Destination[256] = "456";Result = strcpy(Source, Destination); printf("Result = %s\\nSource = %s\\nDestination = %s\\n", Result, Source, Destination); return 0;
}