将 3D 数组作为参数 C 传递时丢失一些值

Losing some values while passing 3D array as a parameter C

我正在编写一个程序,我将 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<br> 1380275029 1480938591 1313169236 1229213507 809322318 893792632 代替 1 2 2 3 2 3 2 3 3 3 4 5

有谁知道为什么会发生这种情况?预先感谢您的帮助。

函数参数声明不正确。您正在向函数传递一个可变长度数组,但将相应的参数声明为具有固定大小的数组。

像这样声明函数

void remove_red( int row_num, int col_num, int image[][col_num][3] );

此外,对于数组的维度,最好使用 size_t.

类型而不是 int 类型

这是一个演示程序。

#include <stdio.h>

void f( size_t m, size_t n, int a[][n][3] )
{
    for ( size_t i = 0; i < m; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            for ( size_t k = 0; k < 3; k++ )
            {
                printf( "%2d ", a[i][j][k] );
            }
            putchar( '\n' );
        }
        putchar( '\n' );
    }
}

int main(void) 
{
    size_t m = 2, n = 5;
    int a[m][n][3];

    for ( size_t i = 0; i < m; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            for ( size_t k = 0; k < 3; k++ )
            {
                a[i][j][k] = i * n * 3 + 3 * j + k;
            }
        }
    }

    f( m, n, a );

    return 0;
}

它的输出是

 0  1  2 
 3  4  5 
 6  7  8 
 9 10 11 
12 13 14 

15 16 17 
18 19 20 
21 22 23 
24 25 26 
27 28 29