how to return 2d array from function c 2b 2b

Solutions on MaxInterview for how to return 2d array from function c 2b 2b by the best coders in the world

showing results for - "how to return 2d array from function c 2b 2b"
Claudia
23 Jul 2020
1 #include <cstdio>
2
3    // Returns a pointer to a newly created 2d array the array2D has size [height x width]
4
5    int** create2DArray(unsigned height, unsigned width)
6    {
7      int** array2D = 0;
8      array2D = new int*[height];
9    
10      for (int h = 0; h < height; h++)
11      {
12            array2D[h] = new int[width];
13    
14            for (int w = 0; w < width; w++)
15            {
16                  // fill in some initial values
17                  // (filling in zeros would be more logic, but this is just for the example)
18                  array2D[h][w] = w + width * h;
19            }
20      }
21    
22      return array2D;
23    }
24    
25    int main()
26    {
27      printf("Creating a 2D array2D\n");
28      printf("\n");
29    
30      int height = 15;
31      int width = 10;
32      int** my2DArray = create2DArray(height, width);
33      printf("Array sized [%i,%i] created.\n\n", height, width);
34    
35      // print contents of the array2D
36      printf("Array contents: \n");
37    
38      for (int h = 0; h < height; h++)
39      {
40            for (int w = 0; w < width; w++)
41            {
42                  printf("%i,", my2DArray[h][w]);
43            }
44            printf("\n");
45      }
46    
47          // important: clean up memory
48          printf("\n");
49          printf("Cleaning up memory...\n");
50          for (int h = 0; h < height; h++) // loop variable wasn't declared
51          {
52            delete [] my2DArray[h];
53          }
54          delete [] my2DArray;
55          my2DArray = 0;
56          printf("Ready.\n");
57    
58      return 0;
59    }
60
queries leading to this page
return 2d array from function in c 2b 2bpassing and returning a 2d array in cppc 2b 2b function to return 2d arrayhow to return a 2d vector in c 2b 2bfunctions 2d array call c 2b 2bfunction take 2d array in c 2b 2bhow do i return a 2d vector from c 2b 2b functionhow to return 2d array from function c 2b 2breturn a 2d array c 2b 2bhow to return 2d a array in from function c 2b 2bc 2b 2b function return 2d arrayreturn a 2d array from a function in c 2b 2bcpp how to return a 2d arrayhow to return a 2d array in a function in c 2b 2bc 2b 2b 2d array returncpp function return 2d arraycpp function that returns 2d arrayreturn 2d array in c 2b 2bhow to call 2d array in function cppc 2b 2b return 2d array from functionreturn a flloat 2d array from a function in c 2b 2bdeclare a function that returns a 2d array c 2b 2bhow to return 2d vector in c 2b 2bcall 2d array in function c 2b 2bhow to call 2d array in function in c 2b 2bhow to return a 2d array in c 2b 2bhow to call a 2d array in a function c 2b 2bc 2b 2b return 2d arrayreturn 2d array c 2b 2breturn 2d cahr array c 2b 2bfunction return 2d vector c 2b 2bcalling function with 2d array c 2b 2breturn 2d array from method c 2b 2breturn 2d array return c 2b 2bhow to return 2d array from function in c c 2b 2bc 2b 2b 2d array returning functionhow to return 2d a array in from c 2b 2bhow to return 2d vector element in c 2b 2breturn 2d array from function c 2b 2breturn 2d array from function cpphow to return a 2d array in c 2b 2b functionreturning a 2d array in cpphow to return a 2d array in a functionhow to return 2d a array in c 2b 2bhow to return 2d array in c 2b 2b functionhow to return 2d array from function c 2b 2b