Sort the pointers by the value that they point to C

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

enter image description here

Here is the code, but he does not know t work, and I donwhat 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;
}
+4
source share
2 answers

Image shown by you is incorrect. After initialization:

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

the state is as follows:

enter image description here

The niz2ptr pointer does not point to an array, but to a pointer. So the indexing you do here:

temp = *(niz2ptr + i);

which is identical:

temp = niz2ptr[i];

will be read from the limits when I am more than one. This is because he is trying to read at the address &nizptr+iinstead niz+i.

, temp int, int.

nizptr , :

temp = nizptr[i];

niz2ptr, , nizptr, it:

temp = (*niz2ptr)[i];

, niz2ptr.

+3

- . . , ; . :

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

( )

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

Source: https://habr.com/ru/post/1654105/


All Articles