简体   繁体   English

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

[英]How do i pass an array of structs by reference to a function?

I need to write a function that would reflect an image by having ever pixel in the image with in a 2D array of structs.我需要编写一个 function ,它可以通过在图像中使用二维结构数组来反映图像。 Below is the function i have written which basically switches the last pixel with the first pixel and so on but i need it to edit the original array and not a copy which is what its currently not doing.下面是我写的 function,它基本上用第一个像素切换最后一个像素,依此类推,但我需要它来编辑原始数组,而不是它当前没有做的副本。 Below is the function within main and how the function is laid out.以下是 main 中的 function 以及 function 的布局方式。 Any input would help!任何输入都会有所帮助!

reflect(height, width, &image);

Function: Function:

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;

        }
    }
}

And the struct is as shown below结构如下图

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

The array of structs is created using this:使用以下命令创建结构数组:

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

For starters the function should be declared either like对于初学者, function 应声明为

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

or like或喜欢

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

or like或喜欢

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

and called like并称为

reflect(height, width, image);

Within the function the loop should look like在 function 内,循环应如下所示

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;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM