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

Arrays 如何通过引用将结构数组传递给函数?,arrays,c,struct,pass-by-reference,swap,Arrays,C,Struct,Pass By Reference,Swap,我需要编写一个函数,通过在二维结构数组中使用图像中的任何像素来反映图像。下面是我编写的函数,它基本上是将最后一个像素与第一个像素切换,以此类推,但我需要它来编辑原始阵列,而不是它当前不做的复制。以下是main中的功能以及该功能的布局。任何输入都会有帮助 reflect(height, width, &image); 功能: void reflect(int height, int width, RGBTRIPLE *image[height][width]) { RGBTRIP

我需要编写一个函数,通过在二维结构数组中使用图像中的任何像素来反映图像。下面是我编写的函数,它基本上是将最后一个像素与第一个像素切换,以此类推,但我需要它来编辑原始阵列,而不是它当前不做的复制。以下是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;

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

    // 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, int width, RGBTRIPLE image[][width]);
void reflect(int height, int width, RGBTRIPLE ( *image )[width]);
或者

void reflect(int height, int width, RGBTRIPLE image[][width]);
void reflect(int height, int width, 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;

    }
}
for(int i=0;i
不要使用二维结构,请使用一维结构。工作起来要容易得多。在这种情况下,编译器不能使用一个参数来指定另一个参数的类型。@tadman我正在学习CS50课程,这就是他们要求我们做的。我已经在1D结构中尝试过了,它可以工作,但我需要使用此方法使其工作。在进一步思考时,为什么不将其视为原始字节数组,并使用偏移量计算和一些
memcpy
j
或您的思考来正确导航它twice@tadman刚刚编辑了这篇文章