What is wrong with strncpy_s() here?
<p>I am reading my a textbook and trying to solve the question given to readers.</p>

<p>The fallowing code is function definition from my source file of my answer.</p>

<p>I want to copy content of character strings to another ones.</p>

<p>I chose functions strncpy_s().</p>

<p>But it does not work.</p>

<p>Microsoft Visual Studio says Debug Assertion Failed!</p>

<p>I have no idea how to fix it.</p>

<blockquote>
  <p>cow.h</p>
</blockquote>

<p>// class declarations</p>

<pre><code>#include &lt;iostream&gt;
#ifndef COW_H_
#define COW_H_

class Cow {
char name[20];
char * hobby;
double weight;
public:
Cow();
Cow(const char * nm, const char * ho, double wt);
Cow(const Cow &amp; c);
~Cow();
Cow &amp; operator=(const Cow &amp; c);
void ShowCow() const;  // display all cow data   
}; 
#endif
</code></pre>

<blockquote>
  <p>cow.cpp</p>
</blockquote>

<p>// class methods</p>

<pre><code>Cow::Cow(const char * nm, const char * ho, double wt)
{
    int len = std::strlen(nm);
    strncpy_s(name, len, nm, len);
    name[19] = '\0';

    len = std::strlen(ho);
    hobby = new char[len + 1];
    strncpy_s(hobby, len, ho, len);
    hobby[len] = '\0';

    weight = wt;
}

Cow::Cow()
{
    strncpy_s(name, 19, "no name", 19);
    name[19] = '\0';

    int len = std::strlen("no hobby");
    hobby = new char[len + 1];
    strncpy_s(hobby, len, "no hobby", len);
    hobby[len] = '\0';

    weight = 0.0;
}

Cow::Cow(const Cow &amp; c)
{
    int len = std::strlen(c.name);
    strncpy_s(name, len, c.name, len);
    name[19] = '\0';

    len = std::strlen(c.hobby);
    hobby = new char[len + 1];
    strncpy_s(hobby, len, c.hobby, len);
    hobby[len] = '\0';

    weight = c.weight;
}

Cow::~Cow()
{
    delete [] hobby;
}

Cow &amp; Cow::operator=(const Cow &amp; c)
{
    if (this == &amp;c)
        return * this;

    delete [] hobby;

    int len = std::strlen(c.name);
    strncpy_s(name, len, c.name, len);
    name[19] = '\0';

    len = std::strlen(c.hobby);
    hobby = new char[len + 1];
    strncpy_s(hobby, len, c.hobby, len);
    hobby[len] = '\0';

    weight = c.weight;
    return * this;
}

void Cow::ShowCow() const
{
    cout &lt;&lt; name &lt;&lt; ", " &lt;&lt; hobby &lt;&lt; ", " &lt;&lt; weight &lt;&lt; endl;  
}
</code></pre>

<blockquote>
  <p>usecow.cpp</p>
</blockquote>

<pre><code>  #include &lt;iostream&gt;
  #include "cow.h"


  int main()
  {
      Cow Japan;
      Japan.ShowCow();

      Cow America("Aspen", "Swim", 307.45);
      America.ShowCow();

      return 0;
  }
</code></pre>