【发布时间】:2020-05-06 13:14:17
【问题描述】:
我正在编写一个程序,其中我将 3d 数组作为参数传递给函数remove_red(),但在第二行之后,数组中的值被一些随机数覆盖。这是代码:
void remove_red(int image[100][100][3], int row_num, int col_num)
{
printf("You are now in remove_red function\n");
for(int i = 0; i < row_num; i++){
for (int j = 0; j < col_num; j++) {
for (int k = 0; k < 3; k++) {
printf("%d ", image[i][j][k]);
}
printf("\n");
}
}
}
int main(int argc, char *argv[]) {
int option;
scanf("%d", &option);
if(option == 1 || option == 2 || option == 3){
char type[3];
int col_num, row_num, contrast;
scanf("%s %d %d %d", type, &row_num, &col_num, &contrast);
printf("Type: %s, columns: %d, rows: %d, contrast: %d, option: %d\n", type, col_num, row_num, contrast, option);
int image[row_num][col_num][3];
int pixel;
for(int i = 0; i < row_num; i++){
for (int j = 0; j < col_num; j++) {
for (int k = 0; k < 3; k++) {
scanf("%d", &pixel);
printf("%d ", pixel);
image[i][j][k] = pixel;
printf("The added number is %d ", image[i][j][k]);
}
printf("\n");
}
}
printf("Type: %s, columns: %d, rows: %d, contrast: %d, option: %d\n", type, col_num, row_num, contrast, option);
if(option == 1){
printf("You chose option %d!\n", option);
//The image 3d array has all values in this part of the code
remove_red(image, row_num, col_num);
}else if(option == 2){
printf("You chose option %d!\n", option);
convert_to_black_and_white(image);
}else if(option == 3) {
printf("You chose option %d!\n", option);
instagram_square(image);
}
}else{
printf("Error: Expecting one command-line argument, which needs to be either 1, 2, or 3.");
return 0;
}
return 0;
}
在remove_red() 中时,我打印得到的数组:
1 2 2 3 2 3
代替
1380275029 1480938591 1313169236 1229213507 809322318 893792632
1 2 2 3 2 3
2 3 3 3 4 5
有谁知道为什么会发生这种情况?提前感谢您的帮助。
【问题讨论】:
-
你的输入是什么?
标签: c function multidimensional-array declaration variable-length-array