简体   繁体   English

[通过rewrite解决]传递指针和递归函数的问题

[英][Solved by rewrite]Problem with passing pointer and recursive function

I'm really newbie to c programing, and I have no idea how can I make it work.我真的是 c 编程的新手,我不知道如何使它工作。 I want to make sort function that sorting array of integers by using 2 argument.我想使用 2 个参数创建对整数数组进行排序的排序函数。 I want to use recursive on sort function.我想在排序函数上使用递归。 and sorting from end of array to first.并从数组末尾排序到第一个。 by ascending order.按升序排列。

void  ft_sorting(int *arr, int size);

but I don't know what went wrong.但我不知道出了什么问题。 It totally out of my understanding with pointer and array.它完全超出了我对指针和数组的理解。 right now I really have no idea.现在我真的不知道。 Can some one pointing what I did wrong.有人可以指出我做错了什么。 And what I need to learn and fix.以及我需要学习和解决的问题。 Thank you!谢谢!

void    ft_recure(int *a, int *b, int j, int k)
{
    if (--j >= 0)
    {
        if (a[k] < b[j])
        {
            a[k] = b[j];
        }
        else
        {
            ft_recure(a[k], b[j], j, k);
        }
    }
    else
        return a[k];
}

void    ft_sort(int *tab, int size)
{
    int i;
    int h;
    while (size > 0)
    {
        i = size;
        h = i;
        tab[size] = ft_recure(tab, tab, i, h);
        size--;
    }
}

and also I try this.我也试试这个。

int  ft_recurs(int x, int y, int a, int b)
{
    int j;
    
    j = a;
  if( a > 0)
  {
    
    if(*x < *(y - 1);)
    {
        b = *(y - 1);
      *x = b;
    }
    ft_recurs(*x,*(y - 1),a - 1, b);
  }
  else
  {
    return *x;
  }
}

void  ft_sort_int_tab(int *tab, int size)
{
  int memo;
  int i;

  while(--size >= 0)
  {
    i = size;
    tab[size] = ft_recurs(tab[i], tab[size], i, memo);
  }
}

In the first approach, you did improper when calling API again:在第一种方法中,您再次调用API时做的不正确:

    void    ft_recure(int *a, int *b, int j, int k)
    {
        if (--j >= 0)
        {
            if (a[k] < b[j])
            {
                a[k] = b[j];
            }
            else
            {
                ft_recure(a[k], b[j], j, k);
            }
        }
        else
            return a[k];
    }

a and b input to API ft_recure is a pointer but in ft_recure(a[k], b[j], j, k); API ft_recure 的 a 和 b 输入是一个指针,但在ft_recure(a[k], b[j], j, k); it is value.这是价值。 You should correct this as: ft_recure(&a[k], &b[j], j, k);您应该将其更正为: ft_recure(&a[k], &b[j], j, k); if you expect to input the address of k and j elements.如果您希望输入 k 和 j 元素的地址。

In your alternative usage:在您的替代用法中:

    int  ft_recurs(int x, int y, int a, int b)
    {
        int j;
        
        j = a;
      if( a > 0)
      {
        
        if(*x < *(y - 1);)
        {
            b = *(y - 1);
          *x = b;
        }
        ft_recurs(*x,*(y - 1),a - 1, b);
      }
      else
      {
        return *x;
      }
    }

The input is value but in the function you are using *x andn *(y-1) is not really a proper way.输入是值,但在您使用的函数中*x andn *(y-1)并不是真正的正确方法。 May be you could try int ft_recurs(int x[], int y[], int a, int b) .也许您可以尝试int ft_recurs(int x[], int y[], int a, int b) But if so, you also need to provide pointer address at ft_recurs(*x,*(y - 1),a - 1, b);但如果是这样,您还需要在ft_recurs(*x,*(y - 1),a - 1, b);处提供指针地址and then the issue come back to similar to first approach.然后问题又回到类似于第一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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