4

I want to sort pointers that point to an array in C, but without moving index positions. Here is a picture of what I am trying to do: enter image description here

enter image description here

Here is the code, but it doesnt work, and I dont know what the problem is.

       #include <stdio.h>
    #include <stdlib.h>

    int main()
    {

      int niz[7] = { 4, 2, 5, 7, 6, 1, 3};


    int j,i;
    int *temp;

    int *nizptr = niz;
    int **niz2ptr = &nizptr;

    for(i = 0; i < 7; i++)
    {
        for(j = 0; j < 7; j++)
        {
            if( niz[i] < niz[j] )
            {
                temp = *(niz2ptr + i);
                *(niz2ptr+i) = *(niz2ptr + j);
                *(niz2ptr+j) = temp;
            }
        }
    }


    for(i = 0; i < 7; i++)
    {
        printf("%d",*(*(niz2ptr + i)));
    }
    return 0;
}
7
  • 2
    What did you find out of your program when you debugged it? Commented Sep 9, 2016 at 8:34
  • 4
    Your code doesn't have an array of pointers like your picture seems to think you do. Maybe start with making your code reflect what your picture is trying to say. Commented Sep 9, 2016 at 8:34
  • 1
    You don't need a pointer to pointer, once you create an array of pointers as commented by WhozCraig, just use array_of_pointers[i], where i = the index to the sorted array of pointers. Commented Sep 9, 2016 at 8:51
  • 3
    Thumbs up for drawing arrows and boxes. Everybody trying to solve issues with lists in C should do that. Commented Sep 9, 2016 at 8:58
  • Did you consider using qsort from the C standard library? Commented Sep 9, 2016 at 9:05

2 Answers 2

3

The image you've shown is not correct. After the initializations:

int *nizptr = niz;
int **niz2ptr = &nizptr;

the state looks like this:

enter image description here

The pointer niz2ptr is not pointing to an array, but to a pointer. Thus the indexing you make here:

temp = *(niz2ptr + i);

which is identical to:

temp = niz2ptr[i];

will read out of bounds of when i is greater than one. This happens because it tries to read at the address &nizptr+i instead of niz+i.

To fix the code, the type of variable temp must be changed to int, not a pointer to int.

Since nizptr is pointing to an array, you can index it:

temp = nizptr[i];

To achieve the same with niz2ptr, first dereference it to get the value of nizptr, and the index it:

temp = (*niz2ptr)[i];

This should be done whenever niz2ptr is used.

Sign up to request clarification or add additional context in comments.

2 Comments

That picture solved my problem i understood after that
There's still just one pointer here, you need an array of pointers in order to sort an array of pointers, as answered by WhozCraig.
1

For some reason you're bent on using a pointer to pointer for this task. None is needed. Your picture shows a value array and a pointer array, where the goal is only the pointer array is changed; the value array stays the same. So do just that:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int niz[7] = { 4, 2, 5, 7, 6, 1, 3};
    int *nizp[7];
    int i;

    // load pointers, show original order with addresses
    for (i=0; i<7; ++i)
    {
        nizp[i] = niz+i;
        printf("%p: %d\n", (const void*)(nizp[i]), *(nizp[i]));
    }
    fputc('\n', stdout);

    // sort the pointer array; not the value array
    int swapped = 1;
    int n = 7;
    while (swapped && n-- > 0)
    {
        swapped = 0;
        for (i=0; i<n; ++i)
        {
            if (*(nizp[i+1]) < *(nizp[i]))
            {
                void *tmp = nizp[i+1];
                nizp[i+1] = nizp[i];
                nizp[i] = tmp;
                swapped = 1;
            }
        }
    }

    // show the sorted pointer array, dereferenced
    for(i = 0; i < 7; i++)
        printf("%p: %d\n", (const void*)(nizp[i]), *(nizp[i]));
    fputc('\n', stdout);

    // prove the original sequence is still unsorted
    for(i = 0; i < 7; i++)
        printf("%p: %d\n", (const void*)(niz+i), niz[i]);
    fputc('\n', stdout);

    return 0;
}

Output (addresses are system dependent)

0x7fff5fbff980: 4
0x7fff5fbff984: 2
0x7fff5fbff988: 5
0x7fff5fbff98c: 7
0x7fff5fbff990: 6
0x7fff5fbff994: 1
0x7fff5fbff998: 3

0x7fff5fbff994: 1
0x7fff5fbff984: 2
0x7fff5fbff998: 3
0x7fff5fbff980: 4
0x7fff5fbff988: 5
0x7fff5fbff990: 6
0x7fff5fbff98c: 7

0x7fff5fbff980: 4
0x7fff5fbff984: 2
0x7fff5fbff988: 5
0x7fff5fbff98c: 7
0x7fff5fbff990: 6
0x7fff5fbff994: 1
0x7fff5fbff998: 3

1 Comment

Now i understood your comment on my post!

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.