简体   繁体   中英

Swapping Pointers of Array

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    void swapNum(int *q, int *p)
    {
        int temp;
        temp = *q;
        *q = *p;
        *p = temp;
    }

    void reverse(int *ip, int const size)
    {
        for (int k = 0; k < size; k++)
        {
            if (k == (size/2))
            {
                int *q = &ip[k];
                int *p = &ip[k+1];
                swapNum(q,p);
                break;
            }
            else
                swap(ip[k], ip[size-k]);
        }
    }

    int main()
    {
        const int size = 20;
        int arr[size];
        int *ip;
        ip = arr;

        cout << "Please enter 20 different numbers." << endl;

        for (int i = 0; i < size; i++)
        {
            cout << "\nNumber " << i+1 << " = ";
            cin >> ip[i];
        }

        reverse(ip, size);

        cout << "I will now print out the numbers in reverse order." << endl;

        for (int j = 0; j < size; j++)
        {
            cout << ip[j] << " ";
        }

        return 0;
    }

When I try to run this program it crashes. I don't know what's wrong and the purpose of my program is to swap number of the array using pointers. I am recently introduced to this so I am not that familiar with it. But I think that I am swapping the address of the numbers instead of swapping the numbers in the address. Correct me if I am wrong.

You're accessing outside the array bounds in reverse() when you do:

swap(ip[k], ip[size-k]);

On the first iteration of the for loop, k is 0 and size-k is size . But array indexes run from 0 to size-1 . So it should be:

swap(ip[k], ip[size-k-1]);

But I don't see a definition of swap in your program. I think it should actually be:

swapNum(&ip[k], &ip[size-k-1]);

Another improvement: Instead of handling size == k/2 specially and using break , just use size < k/2 as the bound test in the for loop.

swap(ip[k], ip[size-k]);

Your problem is there. size - k when k is 0 will lead to undefined behavior (accessing an array out of bounds). Your loop structure in reverse can be simplified:

for (int k = 0; k < size / 2; k++)
    swapNum(&ip[k], &ip[size - k - 1]); // updated to use the address since your swap function takes pointers.

Function reverse is invalid

void reverse(int *ip, int const size)
{
    for (int k = 0; k < size; k++)
    {
        if (k == (size/2))
        {
            int *q = &ip[k];
            int *p = &ip[k+1];
            swapNum(q,p);
            break;
        }
        else
            swap(ip[k], ip[size-k]);
    }
}

For example when k is equal to 0 then you call

            swap(ip[0], ip[size]);

However the array has no element with index size.

ALso you mess two functions std::swap and swapNum

This code snippet also is invalid

        if (k == (size/2))
        {
            int *q = &ip[k];
            int *p = &ip[k+1];
            swapNum(q,p);
            break;
        }

When size is an even number (or an odd number) as in your code then you make incorrect swap. For example if size is equal to 20 then you should swap ip[9[ with ip[10]. However according to the code snippet above you swap ip[10] with ip[11].

You could use standard algorithm std::reverse

for example

#include <algorithm>
#include <iterator>

//...

std::reverse( std::begin( arr ), std::end( arr ) );

or

#include <algorithm>

//...

std::reverse( arr, arr + size );

If you want to write the function yourself then it could look as

void reverse( int a[], int size )
{
    for (int k = 0; k < size / 2; k++)
    {
        int tmp = a[k];
        a[k] = a[size-k-1];
        a[size-k-1] = tmp; 
    }
}

Or if you want to use your function swapNum then

void reverse( int a[], int size )
{
    for (int k = 0; k < size / 2; k++)
    {
        swapNum( &a[k], &a[size-k-1] );
    }
}

EDIT: I removed qualifier const from the first parameter that was a typo.

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