如何通过引用函数传递结构数组?

问题描述

我需要编写一个函数,该函数通过在结构的2D数组中具有图像中的像素来反映图像。下面是我编写的函数,该函数基本上将最后一个像素与第一个像素切换,依此类推,但是我需要它来编辑原始数组,而不是其当前不执行的操作。下面是main中的函数以及该函数的布局。任何输入都会有所帮助!

reflect(height,width,&image);

功能

void reflect(int height,int width,RGBTRIPLE *image[height][width])
{
    RGBTRIPLE temp;
    for ( int i = 0 ; i < height ; i++)
    {
        for( int j = 0 ; j < width ; j++)
        {
            temp = image[i][j];
            image[i][j] = image[i][width-j-1];
            image[i][width-1-j]=temp;

        }
    }
}

结构如下所示

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

结构数组是使用以下方法创建的:

    // Allocate memory for image
    RGBTRIPLE(*image)[width] = calloc(height,width * sizeof(RGBTRIPLE));

解决方法

对于初学者来说,该函数应声明为

void reflect(int height,int width,RGBTRIPLE image[height][width]);

或喜欢

void reflect(int height,RGBTRIPLE image[][width]);

或喜欢

void reflect(int height,RGBTRIPLE ( *image )[width]);

并称呼为

reflect(height,width,image);

在函数中,循环应类似于

for ( int i = 0 ; i < height ; i++)
{
    for( int j = 0 ; j < width / 2 ; j++)
    {
        temp = image[i][j];
        image[i][j] = image[i][width-j-1];
        image[i][width-1-j]=temp;

    }
}