2017-03-03 84 views
1

我有一個2D數組,我們稱之爲「A1」。2D陣列混淆(C程序)

A1[rows][cols]. 

在我的計劃後,我創建另一個二維數組稱爲「A2」,

A2[new_rows][new_cols] 

A2A1大......有沒有什麼辦法,我設置A1相同的大小&內容的A2

+3

如果'A1'是一個靜態數組,不,你不能。如果它是動態分配的,你就有機會。順便說一句,你應該發佈[MCVE](http://stackoverflow.com/help/mcve) – LPs

+1

[memcpy](http://en.cppreference.com/w/cpp/string/byte/memcpy)?看到[這個答案](http://stackoverflow.com/a/16896253/4175042) – Avantol13

+0

@ Avantol13矩陣有不同的大小... – LPs

回答

0

數組在C中是靜態的,所以不幸的是,一旦定義它,你就不能改變數組的大小。但是,您可以使用動態分配的數組來實現您所說的內容(儘管這與調整數組的大小不同,因爲在重新分配時,您將失去對原始數組的引用)。首先創建兩個動態分配的數組A1A2,使用malloc。接下來,使用reallocA1重新分配爲與A2相同的大小。最後,將A2的內容複製到A1。這將有效地「調整」A1A2的大小相同,其內容與A2相同。下面是一些示例代碼(你可以使用任何填充方法是適合你的,我只是用填料):

#include <stdio.h> 
#include <stdlib.h> 

int **make2DArray(int rows, int cols); 
void populate2DArray(int **array, int rows, int cols); 
void print2DArray(int **array, int rows, int cols); 

int main(int argc, char **argv) 
{ 
    int i, j; 
    int rows = 2, cols = 3; 
    int newRows = 4, newCols = 7; 

    // Create two dynamic arrays. 
    int **A1 = make2DArray(rows, cols); 
    int **A2 = make2DArray(newRows, newCols); 

    // Populate the dynamic arrays (however you like). 
    populate2DArray(A1, rows, cols); 
    populate2DArray(A2, newRows, newCols); 

    // Print original arrays. 
    printf("A1 (before):\n"); 
    print2DArray(A1, rows, cols); 
    printf("\nA2 (before):\n"); 
    print2DArray(A2, newRows, newCols); 

    // Reallocate A1 to be same size as A2. 
    int **temp = realloc(A1, sizeof(int *) * newRows); 
    if (temp) 
    { 
    A1 = temp; 
    int *tempRow; 
    for (i = 0; i < newRows; i++) 
    { 
     tempRow = realloc(A1[i], sizeof(int) * newCols); 
     if (tempRow) 
     { 
     A1[i] = tempRow; 
     } 
    } 
    } 

    // Copy contents of A2 to A1. 
    for (i = 0; i < newRows; i++) 
    { 
    for (j = 0; j < newCols; j++) 
    { 
     A1[i][j] = A2[i][j]; 
    } 
    } 

    // Print resized A1 (should be same as A2). 
    printf("\nA1 (after):\n"); 
    print2DArray(A1, newRows, newCols); 
    printf("\nA2 (after):\n"); 
    print2DArray(A2, newRows, newCols); 
} 

int **make2DArray(int rows, int cols) { 
    // Dynamically allocate a 2D array. 
    int **array = malloc(sizeof(int *) * rows); 
    if (array) 
    { 
    for (int i = 0; i < rows; i++) 
    { 
     array[i] = malloc(sizeof(int) * cols); 
    } 
    } 

    return array; 
} 

void populate2DArray(int **array, int rows, int cols) { 
    // Populate a 2D array (whatever is appropriate). 
    int i, j; 
    for (i = 0; i < rows; i++) 
    { 
    for (j = 0; j < cols; j++) 
    { 
     array[i][j] = i + j; 
    } 
    } 
} 

void print2DArray(int **array, int rows, int cols) 
{ 
    // Print a 2D array to the terminal. 
    int i, j; 
    for (i = 0; i < rows; i++) 
    { 
    for (j = 0; j < cols; j++) 
    { 
     printf("%d ", array[i][j]); 
    } 
    printf("\n"); 
    } 
} 

輸出到下面的代碼將是:

A1 (before): 
0 1 2 
1 2 3 

A2 (before): 
0 1 2 3 4 5 6 
1 2 3 4 5 6 7 
2 3 4 5 6 7 8 
3 4 5 6 7 8 9 

A1 (after): 
0 1 2 3 4 5 6 
1 2 3 4 5 6 7 
2 3 4 5 6 7 8 
3 4 5 6 7 8 9 

A2 (after): 
0 1 2 3 4 5 6 
1 2 3 4 5 6 7 
2 3 4 5 6 7 8 
3 4 5 6 7 8 9