【发布时间】: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);?