-2

I'm a little stuck with my school project. So I need to make a dynamic 2-dimensional array. It has to be created in a function with 3 parameters: 2-dimensional char array, length and width. This is what I have so far. Length and width come from another function and there are no problems with that. I feel like I am very close but I don't know how do I save the array to theArray[][] and do I need to create a new variable for where i put /*what here?*/ . Didn't find anything this specific from the web(maybe I just don't know what to search exactly) Thanks in advance!

void doArray(char theArray[][], unsigned int length, unsigned int width) {

char** /*what here?*/ = new char*[lenght];
for(unsigned int i = 0; i < length; i++){
    /*what here?*/[i] = new char[width];
}}

int main() {
unsigned int lenght = 0;
unsigned int width = 0;
char theArray[][];
size(lenght, width);
doArray(theArray, lenght, width);}
reimotaavi
  • 57
  • 6
  • 1
    Why not `std::vector>`? – P.P Apr 11 '20 at 15:31
  • You misunderstood something about your project. As shown, C++ does not work this way. You need to go back and clarify the exact details of what you need to do. If your function has to "make" this array, it obviously can't be a parameter to the same function, it does not exist yet, until the function creates it. As Mr. Spock would say: this is not logical. You need to get a better explanation of what you really have to do and what your real requirements are. – Sam Varshavchik Apr 11 '20 at 15:31
  • A 2-dimensional dynamic array in C++ is: `std::vector> a(SIZE_M, std::vector(SIZE_N));` – Thomas Sablik Apr 11 '20 at 15:32
  • Even though your syntax is missing or off, what you are being taught is practically the worst way to create such an array in C++, even if told that you must use `char **`. Memory is strewn all over the heap, no check if one of those allocations in the `for` loop fails, thus leaking more memory, etc. If you want to see a better way to do this, [see this](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie Apr 11 '20 at 15:38
  • Note: Don't focus hard on finding exact matches. They don't exist all that often, and even when they do it generally leads to [Cargo Cult Programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). You are much better off knowing the rules and how to adapt related examples to meet your needs. Folks who can only program by cut and paste are not very effective, undesirable in the workforce, and easily replaced. – user4581301 Apr 11 '20 at 16:11
  • A practical note: You will find this function easier to write if you return the constructed array rather than taking it as a parameter and updating it. For one thing, if you need to update a parameter in a function, you must pass it by reference; otherwise, you do all the work on a copy and the original back in the calling function is unaware, and often you'll find the compiler optimizes the function out because it performed no externally observable work. – user4581301 Apr 11 '20 at 16:16
  • Does this answer your question? [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Agustín Nieto García Apr 11 '20 at 16:22

4 Answers4

0

A 2-dimensional dynamic array in C++ is: std::vector<std::vector<TYPE>> a(SIZE_M, std::vector<TYPE>(SIZE_N));

#include <vector>

void doArray(auto &theArray, std::size_t length, std::size_t width) {
    theArray = std::vector<std::vector<char>>(length, std::vector<char>(width));
}

int main() {
    std::size_t lenght = 0;
    std::size_t width = 0;
    std::vector<std::vector<char>> theArray;
    size(lenght, width);
    doArray(theArray, lenght, width);
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
0

Another way would be to use malloc.

char **arr;
arr = (char**)malloc(length * sizeof(char*));
for(i=0;i<length;i++){
        arr[i]=(char*)malloc(width * sizeof(char));
}
Alina
  • 1
  • 1
  • 3
  • 1
    The OP couldn't get `new[]` to work, and showing low-level `C` constructs such as `malloc` isn't really helpful, especially when the coder is using C++. – PaulMcKenzie Apr 11 '20 at 16:03
  • Note that `malloc` should be your last choice, and be used only when no other allocation method meets the program requirements.The order of preference should go something like: No dynamic allocation at all (can't be done in this case), library container (like `std::vector`), smart pointer, raw pointer allocated with `new`, raw pointer allocated with `malloc`. – user4581301 Apr 11 '20 at 16:03
0

It's duplicated. See here for a better explanation.

Whatever, this is what you want:

void doArray(char **theArray, unsigned int length, unsigned int width) {
  theArray = new char*[length];
  for(unsigned int i = 0; i < length; ++i)
    theArray[i] = new char[width];
}

Please don't comment I should use size_t instead of unsigned int.

0

When you want a function to create something, the normal way to to return the created object, not to pass it in as a parameter. I think maybe that is what is confusing you. For some reason beginners have often have trouble with the idea of functions returning things.

So rewriting your code to use a return instead of a parameter, you get this

char** doArray(unsigned int length, unsigned int width) {
    char** arr = new char*[length];
    for(unsigned int i = 0; i < length; i++) {
        arr[i] = new char[width];
    }
    return arr;
}

Then in main you call the function like this

int main() {
    char** theArray;
    ...
    theArray = doArray(length, width);

As others have pointed out, the correct way in C++ to do this is to use a vector. But either way the lesson about writing functions to return values applies.

john
  • 85,011
  • 4
  • 57
  • 81