如何使用3D char数组调整双指针2D char数组的大小?

时间:2017-02-17 05:29:42

标签: c++ arrays pointers multidimensional-array function-pointers

我不确定如何从char ***调整char ** 2D数组的大小。 char ** 2D数组通过此调整大小函数传递,如下所示。

这是所说的功能:

    void resize_canvas(char*** Cp, int old_width, int old_height, int new_width, int new_height, char wc){

    int height = new_height;
    int width = new_width;
    int depth = 1;

    Cp = new char**[height];

    for(int i = 0; i < height; i++){
        Cp[i] = new char*[width];

        for(int j = 0; j < width; j++){
            Cp[i][j] = new char[depth];
            for(int z = 0; z < depth; ++z){
                Cp[i][j][z] = wc;
                cout << Cp[i][j][z];
            }
        }
        cout << endl;
    }
}

这将通过以下代码。它打印出一个char ***,但不是我希望它的大小。它必须能够从上面的函数调整到任何给定的大小:

    // C:
    // ddddddd
    // ddddddd
    // ddddddd
    // ddddddd
    // ddddddd
    // ddddddd

它没有给我我正在寻找的东西。之前的char **数组打印出来:

    // C:
    // ddddddeeee
    // ddddddeeee
    // ddddddeeee
    // ddddddeeee
    // ddddddeeee

我需要它才能调整大小,以便以后可以这样做:

char** allocate_canvas(int width, int height){

    char **array;
    char temp = 'A';
    array = (char **)malloc(height* sizeof(char *));
    for (int i = 0; i < height; i++){
        array[i] = (char*)malloc(width* sizeof(char));
    }

    return array;

}

void wash_canvas(char** C, int width, int height, char wc){

    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            C [j][i] = wc;
        }
    }

}

void deallocate_canvas(char** C, int width){

    for(int i = 0; i < width; i++){
        free(C[i]);
        C[i] = NULL;
    }

    free(C);

}

这些是我之前分配和填充char ** 2D数组的函数:

{{1}}

3 个答案:

答案 0 :(得分:0)

由于您使用的是C ++,因此可以传递对指针的引用。这是实现resize_canvas的一种方法。

void resize_canvas(char**& Cp,
                   int old_width, int old_height,
                   int new_width, int new_height, char wc)
{
   int height = new_height;
   int width = new_width;
   int depth = 1;

   // Allocate memory for the new array.
   char** newArray = new char*[height];
   for(int i = 0; i < height; i++)
   {
      newArray[i] = new char[width];
      for(int j = 0; j < width; j++){
         newArray[i][j] = wc;
      }
   }

   // Deallocate the old array.
   char** oldArray = Cp;
   for(int i = 0; i < old_height; i++)
   {
      delete [] oldArray[i];
   }
   delete [] oldArray;

   // Return the new array to the calling function.
   Cp = newArray;
}

话虽如此,最好使用代表Canvas的类。

class Canvas
{
   public:

     Canvas(int width, int height, char wc);

     void wash(char wc);

     void resize(int new_width, int new_height, char wc);

  private:

     // Private data can take different forms.
     // Best to use std::vector.
     std::vector<std::vector<char>> data;
};

答案 1 :(得分:0)

我认为你的问题源于此:

Cp = new char**[height];

就像通过值传递的任何参数一样,更新它会影响本地值,但不会返回您已更改的内容。实际上,你所做的就是从之前获取2D char数组的地址并抛弃它,用一个新的 local 2D char数组替换它。您需要通过参数分配到指向的内存,或者:

*Cp = new char*[height];

答案 2 :(得分:0)

由于您已经拥有必要的功能,因此请重新使用它们。

//assuming that resize means (new_width > old_width)  and (new_height > old_height)
void resize_canvas(char*** Cp, int old_width, int old_height, int new_width, int new_height, char wc)
{
    //char*** Cp : here Cp is address of existing 2D array.


    //[1] Allocate the new array
    char **C_new = allocate_canvas(new_width, new_height);

    //[2] if the (new_width > old_width) and (new_height > old_height) then fill old elements with corresponding 'Cp[i][j]' and all the extra elements with 'wc'.

    int i, j;

    char **C_old = *Cp;  // now 'C_old' will point to you existing 2D array.

    //filling old elements
    for(i = 0; i < old_height; ++i)
    {
        for(j = 0; j < old_width; ++j)
        {
            C_new[i][j] = C_old[i][j];
        }
    }

    //[3] Deallocate the existing array
    deallocate_canvas(C_old, old_width);

    //[4] assign new array to old array
    *Cp = C_new;

    //filling new elements
    for(; i < new_height; ++i)
    {
        for(; j < new_width; ++j)
        {
            C_new[i][j]= wc;            
        }
        cout << endl;
    }

    //check the array by printing
    cout << endl;
    for(i = 0; i < new_height; ++i)
    {
        for(j = 0; j < new_width; ++j)
        {
            cout << C_new[i][j];
        }
        cout << endl;
    }
}