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_s weirdness

strcpy_s weirdness

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
4 Posts 4 Posters 15 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I was playing around in Microsoft Visual C++, and I noticed something weird about this code:

    void setname(char name[80])
    {
    strcpy_s(name, "joebob");

    char name2\[80\];
    strcpy\_s(name2, "joebob");
    

    }

    It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.

    Mircea NeacsuM L CPalliniC 3 Replies Last reply
    0
    • M mike7411

      I was playing around in Microsoft Visual C++, and I noticed something weird about this code:

      void setname(char name[80])
      {
      strcpy_s(name, "joebob");

      char name2\[80\];
      strcpy\_s(name2, "joebob");
      

      }

      It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Remember that in C arrays and pointers are the same thing so the argument name is a pointer to char. For the compiler, your code fragment is the same as:

      void setname(char *name)
      {
      strcpy_s(name, "joebob");

      char name2\[80\];
      strcpy\_s(name2, "joebob");
      

      }

      In the case of the second call to strcpy_s, the compiler can figure out the size parameter through some template magic. For the first one however you have to use the explicit size form.

      Mircea

      1 Reply Last reply
      0
      • M mike7411

        I was playing around in Microsoft Visual C++, and I noticed something weird about this code:

        void setname(char name[80])
        {
        strcpy_s(name, "joebob");

        char name2\[80\];
        strcpy\_s(name2, "joebob");
        

        }

        It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.

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

        It would help when posting questions that you include the exact error message(s), so it makes it clear what your problem is.

        1 Reply Last reply
        0
        • M mike7411

          I was playing around in Microsoft Visual C++, and I noticed something weird about this code:

          void setname(char name[80])
          {
          strcpy_s(name, "joebob");

          char name2\[80\];
          strcpy\_s(name2, "joebob");
          

          }

          It looks like the compiler complains about the first strcpy_s but not the second one. If I comment out the first one, the program compiles fine. Can anyone explain this? BTW, I know strcpy_s should probably take 3 arguments, but I'm trying to understand this weirdness. Thanks.

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

          See, for instance Array Decay In C - GeeksforGeeks[^].

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          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