-1

I am facing problems to make this piece of code work:

char **createCharArray() {
    char **charArray = new char*[PARAM_COUNT];
    for (int i = 0; i < PARAM_COUNT; ++i) {
        charArray[i] = new char[MAXSIZE];
    }
    return charArray;
}
 
void deleteCharArray(char **charArray) {
    for (int i = 0; i < PARAM_COUNT; ++i) {
        delete[] charArray[i];
    }
    delete[] charArray;
}    

int main(){
    char ** test = createCharArray(); 
    char *asd = new char[MAXSIZE];
    cin >> asd;

    for (int i = 0; i < PARAM_COUNT; ++i) {
        strcpy_s(test[i], asd);
    }

    for (int i = 0; i < PARAM_COUNT; ++i) {
        cout << i << " " << test[i] << endl;
    }

    deleteCharArray(test);
    return 0;
}

How do I copy that string into the char array, where am I mistaking?

Edit: As answered by Igor Tandetnik and user17732522 in the comments and Joseph Larson in the reply below, this was solved by adding the buffer argument to the strcpy_s function, making it a total of 3 arguments.

Nik
  • 37
  • 6
  • *How do I copy that string into the char array?* -- What's wrong with the code you have now? Are we to assume that the secret `createCharArray` has no bugs? Please post a [mcve]. – PaulMcKenzie Mar 06 '22 at 17:18
  • 3
    In C++, the right way is to use strings and vectors. – Eljay Mar 06 '22 at 17:21
  • https://pastebin.com/s9CLy3ZU - Here's the function. strcpy_s gives the following message: "could not match char[_size] against char *" – Nik Mar 06 '22 at 17:21
  • `strcpy_s` takes three parameters, not two. – Igor Tandetnik Mar 06 '22 at 17:22
  • @IgorTandetnik It is a MSVC-specific `strcpy_s`, not the standard C one. – user17732522 Mar 06 '22 at 17:23
  • @user17732522 Even so, it takes three parameters, or two parameters of which the first is a char array. The code shown doesn't follow either pattern. – Igor Tandetnik Mar 06 '22 at 17:24
  • _"Cannot use strings and vectors"_ - Then I would use the documentation for `std::string` and create my own `std::string`-like class and only focus on the basic functionality that you need at the moment. – Ted Lyngmo Mar 06 '22 at 17:25
  • @Nik As mentioned above, `strcpy_s` takes either an array of `char`, or three arguments. You cannot use the two argument version with a `char` pointer. Neither of these are standard C++. – user17732522 Mar 06 '22 at 17:26
  • @Nik You still haven't specified what in the code "doesn't work". If it is a compilation error, please post the error message. – PaulMcKenzie Mar 06 '22 at 17:27
  • error C2660: 'strcpy_s': function does not take 2 arguments However when I used it the same way with a locally created char array it worked with 2 arguments – Nik Mar 06 '22 at 17:29
  • okay, i tried adding a 'buffer size' argument in the middle of those 2 and I think it's working now – Nik Mar 06 '22 at 17:32
  • “Cannot use strings and vectors” why? Is that a homework assignment? A programming challenge? Without context this sounds like an xy problem. – Taekahn Mar 06 '22 at 18:50

1 Answers1

1

There are a few things I find troublesome. First, you've seen people say you should use std::string instead of char arrays, and that's true. But new programmers should understand the entire language, and so understanding how to use char arrays has value.

So let's ignore C++ strings and look at your code:

char ** test = createCharArray(); // array of pointers to char arrays
char *asd = new char[MAXSIZE];
cin >> asd;

for (int i = 0; i < PARAM_COUNT; ++i) {
    strcpy_s(test[i], asd);
}

for (int i = 0; i < PARAM_COUNT; ++i) {
    cout << i << " " << test[i] << endl;
}

deleteCharArray(test);
return 0;

Let's start with this. We don't know what createCharArray() does. Is it doing everything it should? Not only should it create an array of char pointers, but the way you're using it, it also needs to create the buffers they each point to. So it might look something like this:

char ** createCharArray() {
   char ** array = new char *[PARAM_COUNT];
   for (int index = 0; index < PARAM_COUNT; ++index) {
        array[index] = new char[MAXSIZE];
   }
   return array;
}

If yours looks remarkably different, you may have issues.

After that, let's look at this:

for (int i = 0; i < PARAM_COUNT; ++i) {
    strcpy_s(test[i], asd);
}

As others have said, this version of strcpy_s takes three arguments: strcpy_s(test[i], asd, MAXSIZE);

Note that you're copying the same string into place multiple times. I wonder if your code should really do this:

for (int i = 0; i < PARAM_COUNT; ++i) {
    cin >> asd;
    strcpy_s(test[i], asd, MAXSIZE);
}

And finally, the delete method needs to delete the individual pointers and then the array of pointers.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36