Pointers Swap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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/2; k++)
    {
        if (!k == (size/2))
            swap(ip[k], ip[size-k]);
        else
        {
            int q = ip[k];
            int p = ip[k+1];
            swapNum(&q,&p);
        }
    }
}

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;
}


I do not know why my numbers do not swap. I am supposed to use pointers to swap my numbers
Last edited on
The numbers in the array do not swap because you are swapping copies of them.
In your code:

1
2
3
4
5
int q = ip[k];
int p = ip[k+1];
swapNum(&q,&p); // q and p are swapped!

// but ip[k] and ip[k+1] are not! 


This is because q and p are new variables which copy the value of ip[k] and ip[k+1].

One solution is to swap ip[k] and ip[k+1] directly.

swapNum(&ip[k], &ip[k+1]);

Another solution is to declare q and p as references.
A reference is an alternative name ("alias") of an existing variable.

1
2
3
int &q = ip[k]; // q is alternative name of ip[k]
int &p = ip[k+1]; // p is alternative name of ip[k+1]
swapNum(&q, &p);


Note that in declaring the references, the ampersand symbol (&) is not the address-of operator used when calling swapNum(). Instead it is part of the declarations of p and q.

Edit: another solution is to declare p and q as pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
int *q = &ip[k];
int *p = &ip[k+1];
swapNum(q, p);

// or do it in a fancy way by using pointer arithmetic

int *q = ip + k;
int *p = ip + k+1;
swapNum(q, p);

// or without p and q

swapNum(ip + k, ip + k+1);
Last edited on
I changed the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void reverse(int *ip, int const size)
{
    for (int k = 0; k < size/2; k++)
    {
        if (k == (size/2))
        {
            int *q = &ip[k];
            int *p = &ip[k+1];
            swapNum(q,p);
        }
        else
            swap(ip[k], ip[size-k]);
    }
}


now when i run the program and enter my 20 numbers, it crashes

Firstly, because the for() looping condition is k < size/2 only the else branch will be entered.

This is because k == size/2 will never be true inside the for() loop.

Secondly, if your program crashes due to a segmentation fault (I did not test your program to see) then it means you are using bad pointers.

A bad pointer points to memory that you may not modify the contents of.

For example:

1
2
3
4
int a[5];

// available: a[0] a[1] a[2] a[3] a[4]
// NOT available: a[5] 


In the code above, trying to modify a[5] may result in a crash because it is not a valid element of array a.

With this in mind, if we look at your code, we see:

12
13
14
15
16
17
swap(ip[k], ip[size-k]); // what if k == 0? then:

swap(ip[0], ip[size]); // oops, ip[size] is invalid just like a[5]

// so the correction is:
swap(ip[k], ip[size - k - 1]);


By the way, you are using std::swap() from the algorithm library.
This is the problem in using namespace std; and why many programmers avoid it.

The algorithm header (which declares std::swap() in C++98) is indirectly included by the iostream header.

Without using namespace std; you would always need to qualify things from the C++ library accordingly: by adding the std:: prefix.

This would improve the clarity of your code: you wouldn't use elements of the C++ library "by mistake" and other programmers would instantly recognize which elements are provided by the C++ library and which ones you wrote yourself.

1
2
3
4
5
6
#include <algorithm>

// using namespace std; // don't

// ...
std::swap(ip[k], ip[size - k - 1]);


Finally, you want to use swapNum(), do you not?
Last edited on
Topic archived. No new replies allowed.