Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. strcpy dilemma - it works half way

strcpy dilemma - it works half way

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiohardwaretoolshelp
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    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 was Result init before strcpy was run. and Source is STILL 123. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA 456 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

    P L CPalliniC A 4 Replies Last reply
    0
    • V 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 was Result init before strcpy was run. and Source is STILL 123. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA 456 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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I 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;

      1 Reply Last reply
      0
      • V 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 was Result init before strcpy was run. and Source is STILL 123. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA 456 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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It 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 since strcpy is supposed to return the pointer to the destination field, Result will now point to the data that Source points to, i.e. "123", but the original data should be untouched.

        1 Reply Last reply
        0
        • V 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 was Result init before strcpy was run. and Source is STILL 123. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA 456 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

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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 other C function, after all).

          In testa che avete, signor di Ceprano?

          V 1 Reply Last reply
          0
          • CPalliniC CPallini

            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 other C function, after all).

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            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(;;);

            CPalliniC 1 Reply Last reply
            0
            • V Vaclav_

              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(;;);

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Result must be declared as pointer to character (char *) because (as already explained by Richard) you are assigning it with strcpy return value. Please note such an assignment is useless since strcpy result is already in Destination array.

              In testa che avete, signor di Ceprano?

              V 1 Reply Last reply
              0
              • CPalliniC CPallini

                Result must be declared as pointer to character (char *) because (as already explained by Richard) you are assigning it with strcpy return value. Please note such an assignment is useless since strcpy result is already in Destination array.

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                Thanks. I know the results are already known, but I was just using it to learn about the strcpy.

                1 Reply Last reply
                0
                • V 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 was Result init before strcpy was run. and Source is STILL 123. Result suppose to be Source ( source destination swap) , but I expect the Source to be COPY of the original Destination AKA 456 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

                  A Offline
                  A Offline
                  Arthur V Ratz
                  wrote on last edited by
                  #8

                  This 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;
                  

                  }

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups