gpt4 book ai didi

C 发送/返回带有函数的数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:56 26 4
gpt4 key购买 nike

我看到了这篇文章 http://www.eskimo.com/~scs/cclass/int/sx5.html

但这部分让我感到困惑:如果我们已经使用 send_arraysend_array_2 修改它,那么在 send_array_3 中返回数组有什么意义例如?我们不需要返回,对吧?

void send_array(int a[], int n) {
for (int i=0; i<n; i++)
a[i] = i*i;
}

void send_array_2(int* a, int n) {
for (int i=0; i<n; i++)
a[i] = i*i;
}

int* send_array_3(int a[], int n) {
for (int i=0; i<n; i++)
a[i] = i*i;
return a;
}

最佳答案

他们说它没那么有用:

Another difference is that the return value of this latest version of itoa isn't terribly useful. The pointer which this version of itoa returns is always the same as the pointer you handed it. Even if this version of itoa didn't return anything as its formal return value, you could still get your hands on the string it created, since it would be sitting right there in your own array (the one that you passed to itoa). In the case of getline, we had a second thing to return as the formal return value, namely the length of the line we'd just read.

通常,您要么将内存分配给指针并在函数中返回数组,您将指针传递给已分配内存的数组,并使用任何返回值进行错误处理,因此您可以将函数包装在 if声明或其他内容。

您甚至可以将数组指针的地址作为 int **(对于 int 数组)分配内存,然后不必返回它。

关于C 发送/返回带有函数的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19351561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com