c语言如何返回一个数组?
extend()函数返回数组amounts怎么改?#include<stdio.h>#include<stdlib.h>doubleextend(double[],dou...
extend()函数返回数组amounts怎么改?
#include<stdio.h>
#include<stdlib.h>
double extend(double[],double[]);
int main(){
double price[] = {10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};
double quantity[] = {4,8.5,6,8.35,9,15.3,3,5.4,2.9,4.8};
double amount[10];
int i;
extend(price,quantity);
for (i = 0; i < 10; i++){
printf("%lf\n", amount[i]);
}
printf("\n");
return 0;
}
double extend(double prices[], double quantities[]){
int i;
double amounts[10];
for (i = 0; i < 10; i++){
amounts[i] = prices[i] * quantities[i];
}
return amounts;
} 展开
#include<stdio.h>
#include<stdlib.h>
double extend(double[],double[]);
int main(){
double price[] = {10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};
double quantity[] = {4,8.5,6,8.35,9,15.3,3,5.4,2.9,4.8};
double amount[10];
int i;
extend(price,quantity);
for (i = 0; i < 10; i++){
printf("%lf\n", amount[i]);
}
printf("\n");
return 0;
}
double extend(double prices[], double quantities[]){
int i;
double amounts[10];
for (i = 0; i < 10; i++){
amounts[i] = prices[i] * quantities[i];
}
return amounts;
} 展开
6个回答
展开全部
只能返回一个数,数组不能返回,以数组为函数参数传给形参时,由于是数组名传递(地址传递),实参和形参共用一段内存,对形参更改时实参的值也会改变,所以不需要返回值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
return amounts;
你返回的是地址,所以函数类型应该是指针型的才能返回。
即 * extend
你返回的是地址,所以函数类型应该是指针型的才能返回。
即 * extend
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
double extend(double prices[], double quantities[]){
int i;
double amounts[10];
for (i = 0; i < 10; i++){
amounts[i] = prices[i] * quantities[i];
}
return amounts; //栈中局部变量,返回后其内存为垃圾值
}
改成:
double *extend(double[],double[]);
int main(){
double price[] = {10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};
double quantity[] = {4,8.5,6,8.35,9,15.3,3,5.4,2.9,4.8};
//double amount[10];
double *amount;
int i;
amount = extend(price,quantity);
for (i = 0; i < 10; i++){
printf("%lf\n", amount[i]);
}
delete[] amount; //释放内存
printf("\n");
return 0;
}
double *extend(double prices[], double quantities[]){
int i;
//double amounts[10];
double *amounts = new double[10];
for (i = 0; i < 10; i++){
amounts[i] = prices[i] * quantities[i];
}
return amounts; //可以返回指针
}}
int i;
double amounts[10];
for (i = 0; i < 10; i++){
amounts[i] = prices[i] * quantities[i];
}
return amounts; //栈中局部变量,返回后其内存为垃圾值
}
改成:
double *extend(double[],double[]);
int main(){
double price[] = {10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};
double quantity[] = {4,8.5,6,8.35,9,15.3,3,5.4,2.9,4.8};
//double amount[10];
double *amount;
int i;
amount = extend(price,quantity);
for (i = 0; i < 10; i++){
printf("%lf\n", amount[i]);
}
delete[] amount; //释放内存
printf("\n");
return 0;
}
double *extend(double prices[], double quantities[]){
int i;
//double amounts[10];
double *amounts = new double[10];
for (i = 0; i < 10; i++){
amounts[i] = prices[i] * quantities[i];
}
return amounts; //可以返回指针
}}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<stdlib.h>
double extend(double[],double[],double *p);
int main()
{
double price[] = {10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};
double quantity[] = {4,8.5,6,8.35,9,15.3,3,5.4,2.9,4.8};
double amount[10];
int i;
int j = extend(price,quantity, amount);
for (i = 0; i < j; i++)
{
printf("%lf\n", amount[i]);
}
printf("\n");
return 0;
}
//传入要返回的数组名,函数返回数组的大小
int extend(double prices[], double quantities[],double *p)
{
int i;
for (i = 0; i < 10; i++)
{
*(p+i)= prices[i] * quantities[i];
}
return i;
}
#include<stdlib.h>
double extend(double[],double[],double *p);
int main()
{
double price[] = {10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98};
double quantity[] = {4,8.5,6,8.35,9,15.3,3,5.4,2.9,4.8};
double amount[10];
int i;
int j = extend(price,quantity, amount);
for (i = 0; i < j; i++)
{
printf("%lf\n", amount[i]);
}
printf("\n");
return 0;
}
//传入要返回的数组名,函数返回数组的大小
int extend(double prices[], double quantities[],double *p)
{
int i;
for (i = 0; i < 10; i++)
{
*(p+i)= prices[i] * quantities[i];
}
return i;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询