0

I am trying to use a pointer for an array for selection sort.

void sort(int size, int *ptr)
{
int temp;
bool swap;
do
{
    swap = false;
    for (int count = 0; count < (size - 1); count++)
    {
        if (*ptr[count] > *ptr[count + 1])
        {
            temp = *ptr[count];
            *ptr[count] = *ptr[count + 1];
            *ptr[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

im getting a lot of errors saying illegal direction because when using * it must be a pointer. I use it in other methods fine its just this one that it has trouble. This is the call im using.

sort(arraySize, numArray);

everything is declared and working in other methods.

5
  • 1
    which lines are you seeing error messages? also it probably should be ptr[count] and not *ptr[count]. ptr is a pointer and not an array of pointers. Commented Feb 10, 2015 at 5:04
  • So change *ptr[count] to just ptr[count]. You do not need to dereference a pointer if you are using array subscript. Pointers can be dereferenced with either the * operator or with an array subscript in most cases. Commented Feb 10, 2015 at 5:10
  • This looks like a bubble sort, not a selection sort. For a selection sort, you'd walk through the array just looking at the elements to find the smallest (not swapping anything yet). Then, when you got to the end of the array so you had found the smallest element in the unsorted part of the array, you'd do one swap from there to the first unsorted spot (and then have one more item sorted). Commented Feb 10, 2015 at 5:10
  • @BucketsOstuff please consider simple errors such as missing semicolon. There should not be any excuse for posting with such mistakes. Commented Feb 10, 2015 at 5:15
  • tht was a typo in posting Commented Feb 10, 2015 at 5:32

5 Answers 5

1

use ptr[] instead of *ptr[] because, ptr is pointer and if used with [] then it returns element at that location like array does.

void sort(int size, int *ptr)
{
int temp;
bool swap;
do
{
    swap = false;
    for (int count = 0; count < (size - 1); count++)
    {
        if (ptr[count] > ptr[count + 1])
        {
            temp = ptr[count];
            ptr[count] = ptr[count + 1];
            ptr[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}
Sign up to request clarification or add additional context in comments.

Comments

0

error is in *ptr[count] this is wrong syntax for a pointer dereferencing.

Do ptr[count] or *(ptr + count)

Comments

0

Here's the compilation errors removed version.

void sort(int size, int *ptr)
{
int temp;
bool swap;
do
{
    swap = false;
    for (int count = 0; count < (size - 1); count++)
    {
        if (ptr[count] > ptr[count + 1])
        {
            temp = ptr[count];
            ptr[count] = ptr[count + 1];
            ptr[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

Comments

0

read also: C++ Using pointers for selection sort function

for more examples: http://www.codemiles.com/c-examples/c-selection-sort-t2916.html

*ptr[count] doesn't mean anything

ptr - pointer *ptr - value - dereferenced

Comments

0

This is the correct structure You must not use * while using the pointer notation of an array, the use of pointer name without '*' itslef refers to the 0 index of pointer array

void sort(int size, int *ptr)
{
int temp;
bool swap;
do
{
    swap = false;
    for (int count = 0; count < (size - 1); count++)
    {
        if (ptr[count] > ptr[count + 1])
        {
            temp = ptr[count];
            ptr[count] = ptr[count + 1];
            ptr[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.