Search In
• More options...
Find results that contain...
Find results in...

Go to solution Solved by Unimportant,
3 hours ago, AndrewZ said:

<snip>

The index operator [] already dereferences the indexed element. Dereferencing it again results in the error.

`if(scores[k] > scores[k+1])`

You can also do arithmetic directly on pointers. So if you need a pointer there is no need to use index operator [] to dereference a index and then take it's address again.

`swap(scores + k, scores + k + 1);`

Although you should be using std::swap instead of rolling your own unless there's a good reason for it.

Hey everyone, I'm trying to implement bubble sort in c++ with the requirement that it uses pointers instead of regular arrays. I have the algorithm entered, but on the line where the if statement checks the if the current element is greater than the next element, I get the error, "Indirection requires pointer operand ('double' invalid)" If anyone knows what's causing this, please let me know, below is the code.

void sort(double *scores, int numberOfScores) {

bool swapped;

for(int i = 0; i < numberOfScores - 1; i++) {

swapped = false;

for(int k = 0; k < numberOfScores - i - 1; k++) {

if(*(scores[k]) > *(scores[k+1])) {

swap(&scores[k], &scores[k+1]);

swapped = true;

}

}

if (!swapped)

break;

}

}

##### Share on other sites

Also, here is the swap function:

void swap(double *x, double *y) {

double temp = *x;

*x = *y;

*y = temp;

}

##### Share on other sites
2 hours ago, AndrewZ said:

if(*(scores[k]) > *(scores[k+1])) {﻿

scores[k] is a double. You can't dereference a double.

1474412270.2748842

##### Share on other sites
3 hours ago, AndrewZ said:

<snip>

The index operator [] already dereferences the indexed element. Dereferencing it again results in the error.

`if(scores[k] > scores[k+1])`

You can also do arithmetic directly on pointers. So if you need a pointer there is no need to use index operator [] to dereference a index and then take it's address again.

`swap(scores + k, scores + k + 1);`

Although you should be using std::swap instead of rolling your own unless there's a good reason for it.

##### Share on other sites
On 4/27/2019 at 4:01 AM, Unimportant said:

The index operator [] already dereferences the indexed element. Dereferencing it again results in the error.

```
if(scores[k] > scores[k+1])```

You can also do arithmetic directly on pointers. So if you need a pointer there is no need to use index operator [] to dereference a index and then take it's address again.

```
swap(scores + k, scores + k + 1);```

Although you should be using std::swap instead of rolling your own unless there's a good reason for it.

Thank you thank you