-5

I'm trying to use strncpy_s to characters from one word to an array (I cannot use strncpy in Visual Studio 2013 and I'm totally new to strncpy_s). I keep getting these errors whatever I do:

Error 1 error C2660: 'strncpy_s' : function does not take 3 arguments

Error 2 IntelliSense: no instance of overloaded function "strncpy_s" matches the argument list argument types are: (char *, char, int)

The purpose of my code is:

If user inputs, for example, "HELLO" (that is, text = HELLO) Then ->

Copy HELLO to first_array [0]
Copy  ELLO to first_array [1]
Copy   LLO to first_array [2]
Copy    LO to first_array [3]
Copy     O to first_array [4]

And here's my code:

int _tmain(int argc, _TCHAR* argv[])
{
    char text[32];
    cin >> text;
    char* first_array[] = {""};
    int n = strlen(text);

    for (int i = 0; i < n; i++)
    {
        strncpy_s(first_array[i], text[i], n-i);
    }
}

EDIT 1. Modified the code a bit more, now the program runs, but after inputing a text, it suddenly gives me the "example.exe stopped working" error.

int _tmain(int argc, _TCHAR* argv[])
{
    char* text[32];
    cin >> *text;
    char* first_array[] = {""};
    //int n = strlen(text);
    int n = sizeof(text);

    for (int i = 0; i < n; i++)
    {
        strncpy_s(first_array[i], n - i, text[i], 32);
    }
  • 7
    [Read the documentation.](https://msdn.microsoft.com/en-us/library/5dae5d43.aspx) –  Feb 17 '15 at 13:56
  • 5
    `'strncpy_s' : function does not take 3 arguments`... there is your answer, right there. The arguments you passed `are: (char *, char, int)`. That's three arguments, and that's wrong. See remyabel's link, which is actually the top hit when you enter "strncpy_s" in a search engine. – DevSolar Feb 17 '15 at 13:56
  • 1
    If this is indeed C++ (as it was originally tagged), then why not use `std::string` and friends? – Biffen Feb 17 '15 at 13:57
  • 1
    Missing #include. @DevSolar to be fair, Microsoft also documents a C++-only 3-argument version. – Marc Glisse Feb 17 '15 at 13:59
  • 1
    Why can't you use `strncpy`? Not saying it would be any better than `strncpy_s`, just a different kind of bad. Anyway, `first_array` is a 1-element array of `char*`, so things are equally bad either way. Also, [avoid `TCHAR`](http://stackoverflow.com/a/3002494). – Deduplicator Feb 17 '15 at 14:01
  • first_array[i] is not a char pointer – paulm Feb 17 '15 at 14:02
  • @paulm actually it seems to be, but it probably shouldn't – o_weisman Feb 17 '15 at 14:03
  • @paulm I think you mean: text[i] is not a char pointer (text+i might be). – Marc Glisse Feb 17 '15 at 14:05

1 Answers1

2

Your code has several issues.

First of all, your call to strncpy_s does not follow the declaration of strncpy_s, which lists four parameters (if the first parameter is a char * as in your case):

errno_t strncpy_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);

But much more importantly, you state that you would like to end up with multiple strings in an array first_array[], each holding a shorter version of the input string than the last. But the first_array[] you declared only holds one char * string, the one you initialized first_array[0] to, which is exactly one character long (the terminating null byte):

char* first_array[] = {""};

Even if you declared it to hold five char * (the initialization is not necessary as you copy the contents over anyway)...

char * first_array[5];

...you still haven't allocated memory space for each of the five char * strings. You just have five pointers pointing nowhere, and would have to allocate memory dynamically, depending on user input.

Because I haven't even talked about what happens if the user enters more than five characters, let alone 32...

At this point, even if I would post "working" code, it would teach you little. You are apparently following some kind of tutorial, or actually attempting to learn by trial & error. I think the right answer here would be:

Get a different tutorial. Even better, get a good book on C or a good book on C++ as online tutorials are notoriously lacking.

Community
  • 1
  • 1
DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Thank you very much DevSolar. The problem with types was exactly what made me suspicious. It's great you pointed it out. But, I still do not know which type to use for first_array and for text. I've experimnted with few and none answered.. Can you please guide me a bit on that? And thanks a heap for the helpful links. Definitely gold. – user2319031232173213123123 Feb 17 '15 at 14:19
  • 1
    @gejak: Your problem is a more general one. Your code exhibits **undefined behaviour** in several places (something C/C++ are very unforgiving about), since you apparently haven't grasped the whole pointer / array / memory allocation thing. At this point, telling you how to solve *this* problem will only set you up for failure later on, which is why I linked the books lists. Take a step back and, instead of trying to make this program of yours work, *learn the language you're using, properly*. Other languages are comparatively "safe" to trial & error in. C/C++ are not. You need to **know**. – DevSolar Feb 17 '15 at 14:23
  • @gejak: In case you're really going for C++, you'd be using `std::string`, and not be using `char *` or `strncpy*()` at all. *But you would still need to know how pointers, arrays, and memory handling **work**,* or you will fail dismally at some other, later problem. Explaining language basics is definitely beyond the scope of a StackOverflow question & answer. – DevSolar Feb 17 '15 at 14:29
  • I'll definitely look into that material. I'm still a beginner, and there is still a lot for me out there to learn, but my urge to learn is the reason of my presence on this website, and that is why I regularly look for different examples from different sources and try my best to do it all on my own. But for this one; I've been working for at least 10 hours on it and haven't been able to find the problem. I've looked into tons of different articles. I really can't figure how I should use std::string for my requirement as for first_array and text. How differently should I use string for each? – user2319031232173213123123 Feb 17 '15 at 14:43
  • 3
    @gejak: Again... **do not try to learn C or C++ from looking at sources**. These languages are best learned from **books**, and for good reason. You have to learn the concepts *behind* the code other people have written, or you will remain blind, or at best one-eyed. Even online tutorials are a bad learning source, because they were usually written *by people in the process of learning*. You don't have the experience to spot bad advice, bad habits, and broken examples yourself. **Get a book and learn this from the ground up.** If this code did cost you 10 hours of your life, you know why. – DevSolar Feb 17 '15 at 14:53