我想撰寫一個復制不同列號陣列的函式。我當前的代碼只能復制具有固定列數的陣列。如何修改我的代碼,以便函式可以復制具有不同列數的陣列?
我的想法是在引數串列中提供不同的列號資訊,但我不確定如何實作。
#include <stdio.h>
#include <stdlib.h>
// simple program to copy a 2d array of numbers
int **array_copy2d(int **src, int rows, int cols) {
// allocate
int **result = malloc(sizeof(int*) * rows);
if (result == NULL) {
return NULL;
}
// copies from src to dest array.
for (int row = 0; row < rows; row ) {
// allocate a row
result[row] = malloc(sizeof(int) * cols);
if (result[row] == NULL) {
return NULL;
}
// copy a row.
for (int col = 0; col < cols; col ) {
result[row][col] = src[row][col];
}
}
return result;
}
// driver code for array copy
int main(void) {
// declare and build 2d array
int rows = 3;
int cols = 2;
int row0[] = { 1, 2 };
int row1[] = { 3, 4 };
int row2[] = { 5, 6 };
int *vals[3];
// build vals
vals[0] = row0;
vals[1] = row1;
vals[2] = row2;
// destination array
int **val_copy;
// copy
val_copy = array_copy2d(vals, rows, cols);
// check if malloc worked
if (val_copy == NULL) {
printf("allocation error\n");
return 1;
}
// test
for (int row = 0; row < rows; row ) {
for (int col = 0; col < cols; col ) {
printf("%d ", val_copy[row][col]);
}
printf("\n");
}
return 0;
}
測驗樣品:
1 2
3 4
5 6
謝謝大家的幫助。
uj5u.com熱心網友回復:
int rows, cols;
scanf("%d%d", &rows,&cols);
int **vals = malloc(sizeof(int*)*rows);
for(int row=0; row<rows; row ){
vals[row] = malloc(sizeof(int*)*cols);
for(int col=0; col<cols; col ){
scanf("%d", &vals[row][col]);
}
}
int **val_copy = malloc(sizeof(int*)*rows);
val_copy = array_copy2d(vals, rows, cols);
if (val_copy == NULL) {
printf("allocation error\n");
return 1;
}
for (int row = 0; row < rows; row ) {
for (int col = 0; col < cols; col ) {
printf("%d ", vals[row][col]);
}printf("\n");
}
uj5u.com熱心網友回復:
您當前的代碼可以復制子矩陣:目標矩陣中的行數和列數必須小于或等于源矩陣中的行數和列數。
如果分別傳遞源矩陣和目標矩陣的維度,則可以修改代碼以允許更大的數字。
如果要處理鋸齒狀陣列,即:如果源矩陣中的列數從一行到下一行不同,則必須將行寬作為陣列單獨傳遞。
這是任意尺寸的修改版本:
#include <stdio.h>
#include <stdlib.h>
// simple program to copy a 2d array of numbers
int **array_copy2d(int row1, int col1, int **src, int rows, int cols) {
// allocate
int **result = calloc(sizeof(int*), rows1);
if (result == NULL) {
return NULL;
}
for (int row1 = 0; row < rows1; row ) {
// allocate a row
result[row] = calloc(sizeof(int), cols1);
if (result[row] == NULL) {
return NULL;
}
}
// copies from src to dest array.
if (rows > rows1)
rows = rows1;
if (cols > cols1)
cols = cols1;
for (int row = 0; row < rows; row ) {
// copy a row.
for (int col = 0; col < cols; col ) {
result[row][col] = src[row][col];
}
}
return result;
}
// driver code for array copy
int main(void) {
// declare and build 2d array
int rows = 3;
int cols = 2;
int row0[] = { 1, 2 };
int row1[] = { 3, 4 };
int row2[] = { 5, 6 };
int *vals[3] = { row0, row1, row2 };
// destination array
int **val_copy;
// copy
val_copy = array_copy2d(2, 4, vals, rows, cols);
// check if malloc worked
if (val_copy == NULL) {
printf("allocation error\n");
return 1;
}
// test
for (int row = 0; row < 2; row ) {
for (int col = 0; col < 4; col ) {
printf("%d ", val_copy[row][col]);
}
printf("\n");
}
return 0;
}
輸出:
1 2 0 0
3 4 0 0
uj5u.com熱心網友回復:
我的想法是在引數串列中提供不同的列號資訊
看看可變引數函式
但請注意,在這種情況下,您可以使用陣列的第一個元素作為標記:
int row0[] = {2, 1, 2}; // 2 elements
int row1[] = {3, 3, 4, 5}; // 3 elements
int row2[] = {1, 6}; // 1 element
然后,您的函式可能如下所示:
int** array_copy2d(int **src, int rows) {
// allocate
int** result = malloc(sizeof(int*) * rows);
if (result == NULL) {
return NULL;
}
// copies from src to dest array.
for (int row = 0; row < rows; row ) {
// allocate a row
int cols = src[row][0] 1; // Get the sentinel value
result[row] = malloc(sizeof(int) * cols);
if (result[row] == NULL) {
return NULL;
}
// copy a row.
for (int col = 0; col < cols; col ) {
result[row][col] = src[row][col];
}
}
return result;
}
現在您不需要傳遞列數,在main,切換到
val_copy = array_copy2d(vals, rows);
但請記住跳過列印回圈中的第一個元素:
for (int row = 0; row < rows; row ) {
int cols = val_copy[row][0];
for (int col = 1; col <= cols; col ) {
printf("%d ", val_copy[row][col]);
}
printf("\n");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485115.html
標籤:C
上一篇:將記憶體重新分配給字串的問題
