简体   繁体   中英

Arrays of pointer dereferencing

I want to a function named sortPointers() that sets an array of integer pointers to point to the elements of another array in ascending order.

在此处输入图片说明

What I have done so far is

      void sortP(int src[], int *ptrs[], int n)
 {
     int temp;
    for(int i = 0; i< n ; i++)
    {
        ptrs[i] = & src[i]; // assign the address of each number in the src[] to the array of pointers
    }

    while (1)
    {
        int flag = 0;
        for(int i = 0; i< n;i++)
            {
                    if ( *(ptrs[i]) > *(ptrs[i+1])) //bubble sort
                {
                     temp = *(ptrs[i]);
                     *(ptrs[i]) = *(ptrs[i+1]);
                     *(ptrs[i+1]) = temp;
                     flag = 1;
                }
            }
            if(flag == 0);
            break;
      }

      for(int i = 0; i< n;i++)
      {
          printf("%i\n",ptrs[i]);
      }
 }

In main function , I call this function

main()
{
    int a[5] = {5,4,3,2,1};
    int *ptrs[5]= {&a[0],&a[1],&a[2],&a[3],&a[4]};
 sortP(a, *ptrs, 5);

}

My result are addresses, If I want to print out the actual value that the pointers point to (1,2,3,4,5) ,what should I change in the printf()?

THanks

PS I try *ptrs[i] before , but I got strange number though , not the ones in src[]..

My result are addresses

Technically, your results are undefined behavior, because %i expects an int , not an int* .

Fixing this problem is simple: add a dereference operator in front of ptrs[i] , like this:

for(int i = 0; i< n;i++) {
    printf("%i\n", *ptrs[i]);
}

I got strange number though , not the ones in src[]

The real problem with your code is that you are swapping pointers incorrectly. In fact, you can tell that it's incorrect simply by looking at temp : it needs to be int* , not int and the dereferences on the swap need to go away.

see annotations :

void sortP(int src[], int *ptrs[], int n)
{
    int temp;
    for(int i = 0; i< n ; i++)
    {
        ptrs[i] = & src[i]; // assign the address of each number in the src[] to the array of pointers
    }

    while (1)
    {
        int flag = 0;

        // check if i < n-1, not n
        for(int i = 0; i< n-1;i++)
            {
                if ( *(ptrs[i]) > *(ptrs[i+1])) //bubble sort
                {
                     temp = *(ptrs[i]);
                     *(ptrs[i]) = *(ptrs[i+1]);
                     *(ptrs[i+1]) = temp;
                     flag = 1;
                }
            }
            if(flag == 0)
            break;
      }

      for(int i = 0; i< n;i++)
      {
          //*ptrs[i] instead of ptrs[i]
          printf("%i ",*ptrs[i]);
      }
}

int main(void)
{
    int a[5] = {5,4,3,2,1};
    int *ptrs[5];//= {&a[0],&a[1],&a[2],&a[3],&a[4]};
    sortP(a, ptrs, 5);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM