简体   繁体   中英

2D array horizontal reflection in c

For the reflection code I need to reflect a picture vertically (most left go to most right). But this code below does not change a thing. (I also tried to give up on pointers and use the arrays itself in swap function but did not change a thing either.)

REFLECT:

void swap(RGBTRIPLE* p, RGBTRIPLE* q)
{
    RGBTRIPLE* temp;
    temp = p;
    p = q;
    q = temp;
}


    // 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++)
            {
                swap(&image[i][j],&image[i][width-1-j]);
            }
        }
    }

Your swap function should change to:

void swap(RGBTRIPLE* p, RGBTRIPLE* q)
{
    RGBTRIPLE temp; // use a value temp instead of pointer temp
    temp = *p; // you should swap the value that pointer points to.
    *p = *q;
    *q = temp;
}

If you want to swap the pointers themselves, the function will be as below:

void swap(RGBTRIPLE** p, RGBTRIPLE** q)
{
    RGBTRIPLE* temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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