如何解决如何通过引用函数传递结构数组?
我需要编写一个函数,该函数通过在结构的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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。