[英]How do i pass an array of structs by reference to a function?
我需要编写一个 function ,它可以通过在图像中使用二维结构数组来反映图像。 下面是我写的 function,它基本上用第一个像素切换最后一个像素,依此类推,但我需要它来编辑原始数组,而不是它当前没有做的副本。 以下是 main 中的 function 以及 function 的布局方式。 任何输入都会有所帮助!
reflect(height, width, &image);
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;
}
}
}
结构如下图
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
使用以下命令创建结构数组:
// Allocate memory for image
RGBTRIPLE(*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));
对于初学者, function 应声明为
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]);
并称为
reflect(height, width, image);
在 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.