Search code examples
cfunctionmultidimensional-arraydeclarationvariable-length-array

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


I am writing a program where I am passing 3d array as a parameter to a function remove_red() but after the second row, the values in the array are being overwritten by some random numbers. This is the code:

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;
}


When in remove_red() I print the array I get: 1 2 2 3 2 3
1380275029 1480938591 1313169236 1229213507 809322318 893792632
instead of 1 2 2 3 2 3 2 3 3 3 4 5

Does anyone know why it may happen? Thank you in advance for any help.


Solution

  • The function parameters are declared incorrectly. You are passing to the function a variable length array but declare the corresponding parameter as an array with fixed sizes.

    Declare the function like

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

    Also instead of the type int for dimensions of the array it is better to use the type size_t.

    Here is a demonstrative program.

    #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;
    }
    

    Its output is

     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