Dynamic memory allocation of 2-D array
-
Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL
-
Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL
int twoDArray = new int[rows*cols];
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --If you find my post helpful then do rate it. --Jesus is Lord:rose:
-
Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL
-
Hi, I'd like to write a function, which returns an array of strings. But the size of the array is unknown. For example, it has to create a string array with the nos. I enter (say, "1", "2", "4", "9".. , and return back to the caller function when I enter 0. It has to grow dynamically. But I don't want to do it with built-in classes like list, vector and so on. I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution. Thanks in advance, Sarvan AL
-
Google for the head line of your question:
Dynamic memory allocation of 2-D array
I got 199000 hits, several of them uses malloc. -
I quote the original poster: "I'd like to use 'new' operator or 'malloc/realloc' functions. Pls suggest me a good solution." The original poster is totally free to use any technique available, as far as I'm concerned. I'm not responsible neither for the hits gotten from Google nor what sample the original poster uses. But your teacher has got a point. In C++, it's preferred. I plain C, however, new() isn't available. And that's where malloc/realloc comes in, amongst others.