用C语言测试指针

用C语言测试指针,c,pointers,cs50,C,Pointers,Cs50,我对编码是新手,大约一个月前开始了我的旅程。我刚刚完成了CS50的习题集4,但我想玩一下指针。关于学术诚信-我已经提交了这个问题,这只是它的一个不同版本 这是一个反射过滤器,用于镜像图像RGB过滤器包含图像中每个像素的红色、绿色和蓝色。 对于每一行,这应该交换水平相对边上的像素。对照片中的每一行重复 我试图找出我是否可以使用指针来解决这个问题,但它似乎不起作用。这种方法不就是交换第一个像素的地址和最后一个像素的地址吗?有没有可能用这种方法来解决呢?同样,已经提交了这个特定的集合,只是想了解指针在

我对编码是新手,大约一个月前开始了我的旅程。我刚刚完成了CS50的习题集4,但我想玩一下指针。关于学术诚信-我已经提交了这个问题,这只是它的一个不同版本

这是一个反射过滤器,用于镜像图像RGB过滤器包含图像中每个像素的红色、绿色和蓝色。 对于每一行,这应该交换水平相对边上的像素。对照片中的每一行重复

我试图找出我是否可以使用指针来解决这个问题,但它似乎不起作用。这种方法不就是交换第一个像素的地址和最后一个像素的地址吗?有没有可能用这种方法来解决呢?同样,已经提交了这个特定的集合,只是想了解指针在行动中的作用,以及在他们的帮助下是否有办法做到这一点。谢谢你的帮助

// Reflect image horizontally
   void reflect(int height, int width, RGBTRIPLE image[height][width])

for (int i = 0; i < height; i++)

    for (int j = 0; j < width / 2; j++)

        // find color values for first pixel
        int first_red = image [i][j].rgbtRed;
        int* pfirst_red = &first_red;

        int first_green = image [i][j].rgbtGreen;
        int* pfirst_green = &first_green;

        int first_blue = image [i][j].rgbtBlue;
        int* pfirst_blue = &first_blue;

        // find color values for last pixel
        int last_red = image[i][width - j - 1].rgbtRed;
        int* plast_red = &last_red;

        int last_green = image[i][width - j - 1].rgbtGreen;
        int* plast_green = &last_green;

        int last_blue = image[i][width - j - 1].rgbtBlue;
        int* plast_blue = &last_blue;
        
        // Convert first pixel to last
        pfirst_red = &last_red;
        pfirst_green = &last_green;
        pfirst_blue = &last_blue;

        // Convert last pixel to first
        plast_red = &first_red;
        plast_green = &first_green;
        plast_blue = &first_blue;


        
        :( reflect correctly filters 1x2 image
           expected "0 0 255\n255 0...", not "255 0 0\n0 0 2..."
        :( reflect correctly filters 1x3 image
           expected "0 0 255\n0 255...", not "255 0 0\n0 255..."
        :) reflect correctly filters image that is its own mirror image
        :( reflect correctly filters 3x3 image
           expected "70 80 90\n40 5...", not "10 20 30\n40 5..."
        :( reflect correctly filters 4x4 image
           expected "100 110 120\n7...", not "10 20 30\n40 5..."
//水平反射图像
空心反射(整数高度、整数宽度、RGB三重图像[高度][宽度])
对于(int i=0;i
首先,我会检查
RGBTRIPLE
的声明,因为其成员的大小可能不是
int
。当使用大小不正确的指针时,相邻变量将被覆盖并损坏代码

我在谷歌上找到了这个,但是检查一下你的环境

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
}
RGBTRIPLE;
在代码中,除了有一个指向大小不正确的项的指针外,还声明了一个局部变量,然后指向is地址

int last_red = image[i][width - j - 1].rgbtRed;
int* plast_red = &last_red;   // plast_red is a pointer to last_red
这只允许您交换局部变量的值

您应该从结构中的数据获取地址:

RGBTRIPPLE* ptriplet= last_red = image[i][width - j - 1];  // This is an alternative pointer to the struct
BYTE* plast_red = &image[i][width - j - 1].rgbtRed;
然后使用指针和临时变量交换:

BYTE temp = *plast_red;
*plast_red = *pfirst_red;
*pfirst_red = temp;

pfirst\u red=&last\u red;
所有的
pfirst\u
变量都是本地指针。更改它们不会以任何方式更改它们指向的内容。您需要类似于
*pfirst\u red=*last\u red;
的内容,然后阅读C编译器(可能…)和调试器(可能…)的文档。请参阅并…在上的现有代码中寻找灵感,在下一个问题中提供一些,并使用所有警告和调试信息编译代码,因此
gcc-Wall-Wextra-g
如果使用Oh-wow,似乎比我想象的要复杂。尽管如此,还是有道理的。感谢您的帮助!@abbanator欢迎您,如果您喜欢答案,并且答案是肯定的这是你的问题,你可以把它标记为已接受。