【问题标题】:Returning array[] from void, C从 void,C 返回数组 []
【发布时间】:2013-03-25 19:00:36
【问题描述】:

所以当我写一个函数时

void sort (int oldarray[], int length)
{

//imagine there is a function here that runs a loop and finishes with a sorted:

newarray[];
}

如何让 newarray[] 替换 main 函数中的 oldarray[],如下所示:

int main()
{
int length = 7
int oldarray[length]

//here would be a loop that populates the oldarray

sort(oldarray[], length)

//a loop that prints the newarray[] from the sort or main function
}

仅供参考,这不是家庭作业。我是在自学,所以你不会帮我骗取教授的血汗钱。

【问题讨论】:

  • 如果不就地排序,可以memcpy排序后的数组到原点。
  • 如果你已经有一个数组,为什么还要替换?您可以对oldarray[] 元素进行操作。
  • 但它(即newarray)不会像现在定义的那样超出main()的范围吗?
  • 如果您不想从函数中返回它,请将 newarray 设为全局。
  • Daniel 对memcpy 的建议是正确的。但是,您是否有理由希望 sort() 无效而不是返回 newarray 并分配 oldarray = sort(oldarray, length);

标签: c arrays void


【解决方案1】:

以下是基于 Aniket 的回答,但经过简化:

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

void sort (int *oldarray, int *newarray, int length)
{
    // do your stuff, and put result in newarray
}

int main()
{
    int length = 7;
    int oldarray[length];
    int newarray[length];

    // here would be a loop that populates the oldarray

    sort(oldarray, newarray, length);

    // a loop that prints the newarray from the sort or main function

    return 0;
}

【讨论】:

    【解决方案2】:
    void sort (int *oldarray, int length, int *newarray, int *newlength)
    {
    
    //imagine there is a function here that runs a loop and finishes with a sorted:
    
    //newarray after sorting can be passed to `main` function - even if the function returns void
    // also remember to set the `newlength`
    }
    
    int main()
    {
      int newlength;
      int *newarray = malloc(7 * sizeof(int));
      int length = 7
      int oldarray[length]
    
      //here would be a loop that populates the oldarray
    
      sort(oldarray[], length, newarray, &newlength)
    
      //a loop that prints the newarray[] from the sort or main function
      free(newarray);
      return 0;
    }
    

    【讨论】:

    • 那么如果传入一个指针作为参数,会在main函数中返回或更改?
    • @user2208569,Aniket 想要说明的是指针是静态的,它们指向固定的内存位置,而您实际上可以更改它们指向的内容。
    【解决方案3】:

    您不想在排序调用中加上 []:

    sort(oldarray, length)
    

    如果你真的不想从排序函数返回任何东西而不是传入一个数组,这实际上只是一个指针,你想传入一个指向指针的指针,然后重新分配指针指向的内容到(呼)。像这样:

    int ** pointer_to_arr = &old; //& gives address of old
    sort(pointer_to_arr, length);
    

    按排序:

    sort(int** arr, int len) {
        //you need to malloc the new array if you don't want it
        //to go away on function return:
        int* new_array = (int*) malloc(len*sizeof(int));
        //... sort here into new_array ...
        *arr = new_array; //set arr to the newly sorted array 
    }
    

    您现在可以从 pointer_to_old 访问 new_array:

    int* new_array = *pointer_to_arr;
     //... do what you will
    //don't forget to release you memory when you're done
    free (new_array);
    

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2017-07-13
      • 2015-01-24
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多