gpt4 book ai didi

c - 使用指针传递 2D 数组 (C)

转载 作者:行者123 更新时间:2023-11-30 20:36:28 24 4
gpt4 key购买 nike

下面是我正在使用的代码。当我运行它并注释掉处理 addArray 函数的代码时,效果非常好。

我相信我没有在 addArrays 函数中正确使用指针。任何帮助将不胜感激。

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

void addArrays(int rowSize, int columnSize, int Array1[rowSize][columnSize],
int Array2[rowSize][columnSize],
int *sumloc[rowSize][columnSize]);
void printArray(int rowSize, int columnSize, int arrayValue[rowSize][columnSize]);

/*
This program will make 2 2D matrices out of a random number of rows, 3 columns, and a random set of values.
Then, the two matrices will be added and all three printed.
*/

int main() {
//To get a true random number
srand(time(NULL));

int rowSize = rand() % 4 + 1;
int columnSize = 2;
int Array1[rowSize][columnSize];
int Array2[rowSize][columnSize];
int Arraysum[rowSize][columnSize];

//For loop counter
int col;
int row;

//Makes a unique number for each part of both arrays from 0-50
for (row = 0; row <= rowSize; row++)
for (col = 0; col <= columnSize; col++)
Array1[row][col] = rand() % 50;

for (row = 0; row <= rowSize; row++)
for (col = 0; col <= columnSize; col++)
Array2[row][col] = rand() % 50;


//Add arrays
addArrays(rowSize, columnSize, Array1[rowSize][columnSize],
Array2[rowSize][columnSize],v&Arraysum[rowSize][columnSize]);

//Utilizes the print function
printArray(rowSize, columnSize, Array1);
printArray(rowSize, columnSize, Array2);
printArray(rowSize, columnSize, &Arraysum);

return 0;
}

void addArrays(int rowSize, int columnSize, int a1[rowSize][columnSize],
int a2[rowSize][columnSize],
int *sumloc[rowSize][columnSize]) {
int row;
int col;
sumloc[rowSize][columnSize] = malloc(rowSize * columnSize * sizeof(int));

for (row = 0; row <= rowSize; row++)
for (col = 0; col <= columnSize; col++)
*sumloc[row][col] = a1[row][col] + a2[row][col];

return;
}

void printArray(int rowSize, int columnSize, int arrayValue[rowSize][columnSize]) {
int row;
int col;

for (row = 0; row <= rowSize; row++) {
printf("\n");
printf("[");
for (col = 0; col <= columnSize; col++) {
printf(" %d ", arrayValue[row][col]);
}
printf("]");
}
printf("\n\n");

return;
}

最佳答案

您的代码有几个问题:

  • 您不应该使用<=在你的for循环时,您可以访问数组末尾之外的元素。
  • 行数和列数均为一。
  • 您应该直接传递数组,而不是传递数组元素或暂时传递目标数组的地址。

这是更正后的版本:

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

void addArrays(int rowSize, int columnSize, int Array1[rowSize][columnSize],
int Array2[rowSize][columnSize],
int Result[rowSize][columnSize]);
void printArray(int rowSize, int columnSize, int arrayValue[rowSize][columnSize]);

/*
This program will make 2 2D matrices out of a random number of rows
and 3 columns, and a random set of values.
Then, the two matrices will be added and all three printed.
*/

int main() {
//To get a pseudo random number
srand(time(NULL));

int rowSize = rand() % 4 + 1; // number between 1 and 4
int columnSize = 3;
int Array1[rowSize][columnSize];
int Array2[rowSize][columnSize];
int Arraysum[rowSize][columnSize];

//Makes a unique number for each part of both arrays from 0-50
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < columnSize; col++) {
Array1[row][col] = rand() % 50;
Array2[row][col] = rand() % 50;
}
}

//Add matrices
addArrays(rowSize, columnSize, Array1, Array2, Arraysum);

//Utilizes the print function
printArray(rowSize, columnSize, Array1);
printArray(rowSize, columnSize, Array2);
printArray(rowSize, columnSize, Arraysum);
return 0;
}

void addArrays(int rowSize, int columnSize, int a1[rowSize][columnSize],
int a2[rowSize][columnSize], int result[rowSize][columnSize]) {
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < columnSize; col++)
result[row][col] = a1[row][col] + a2[row][col];
}
}

void printArray(int rowSize, int columnSize, int array[rowSize][columnSize]) {
for (int row = 0; row < rowSize; row++) {
printf("\n");
printf("[");
for (int col = 0; col < columnSize; col++) {
printf(" %d ", array[row][col]);
}
printf("]");
}
printf("\n\n");
}

关于c - 使用指针传递 2D 数组 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35993478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com