C 我想使用指针从函数返回多个值,但它';返回一个值
我想从用户定义的函数返回多个值,但它只返回一个变量。代码如下:C 我想使用指针从函数返回多个值,但它';返回一个值,c,C,我想从用户定义的函数返回多个值,但它只返回一个变量。代码如下: #include <stdio.h> double sum_n_avg(double, double, double, double*, double*); int main() { double n1, n2, n3, sump, avgp; printf("enter numbers:"); scanf("%lf%lf%lf", &n1, &n2, &n3);
#include <stdio.h>
double sum_n_avg(double, double, double, double*, double*);
int main()
{
double n1, n2, n3, sump, avgp;
printf("enter numbers:");
scanf("%lf%lf%lf", &n1, &n2, &n3);
printf("sum and average is %lf",sum_n_avg(n1, n2, n3, &sump, &avgp));
return 0;
}
double sum_n_avg(double n1, double n2, double n3, double *sump, double *avgp)
{
*sump = n1 + n2 + n3;
*avgp = *sump / 3;
}
#包括
平均双和(双,双,双,双,双*,双*);
int main()
{
双n1,n2,n3,集水坑,avgp;
printf(“输入数字:”);
扫描频率(“%lf%lf%lf”、&n1、&n2、&n3);
printf(“总和和平均值为%lf”,总和和平均值(n1、n2、n3、集水坑和平均值));
返回0;
}
双和平均值(双n1、双n2、双n3、双*集水坑、双*平均值)
{
*集水坑=n1+n2+n3;
*avgp=*集水坑/3;
}
您的代码具有未定义的行为,如sum\u n\u avg()
只返回printf(“sum and average is%lf”,?)代码>需要一个值
但你期待着这样的事情
void sum_n_avg(double n1,double n2,double n3,double *sump,double *avgp) {
*sump=n1+n2+n3;
*avgp=*sump/3;
}
int main() {
double n1,n2,n3,sump,avgp;
printf("enter numbers:");
scanf("%lf%lf%lf",&n1,&n2,&n3);
sum_n_avg(n1,n2,n3, &sump,&avgp); /* call like this */
printf("sum and average is %lf and %lf\n",sump,avgp);/*sump and avgp gets updated from sum_n_avg() function */
return 0;
}
您的代码具有未定义的行为,如sum\u n\u avg()
只返回printf(“sum和average是%lf”,?)代码>需要一个值
但你期待着这样的事情
void sum_n_avg(double n1,double n2,double n3,double *sump,double *avgp) {
*sump=n1+n2+n3;
*avgp=*sump/3;
}
int main() {
double n1,n2,n3,sump,avgp;
printf("enter numbers:");
scanf("%lf%lf%lf",&n1,&n2,&n3);
sum_n_avg(n1,n2,n3, &sump,&avgp); /* call like this */
printf("sum and average is %lf and %lf\n",sump,avgp);/*sump and avgp gets updated from sum_n_avg() function */
return 0;
}
您可以在实现时通过指向输出参数的指针返回两个值。
要清理代码,请执行以下操作:
- 如果不返回任何内容,请不要声明返回值:
双和平均值(
->无效和平均值(
),在原型和定义中
- 打印前调用函数,获取指向变量的值:
sum\u n\u avg(n1、n2、n3、集水坑和avgp);printf(
- 将变量作为值提供给printf:
printf(“总和为%lf,平均值为%lf”,油底壳,平均值);
我只是注意到他在早些时候已经有了基本的诀窍。我没有使用它(因为错过了返回而分心),但仍然
归功于他。您可以在实现时通过指向输出参数的指针返回两个值。
要清理代码,请执行以下操作:
- 如果不返回任何内容,请不要声明返回值:
双和平均值(
->无效和平均值(
),在原型和定义中
- 打印前调用函数,获取指向变量的值:
sum\u n\u avg(n1、n2、n3、集水坑和avgp);printf(
- 将变量作为值提供给printf:
printf(“总和为%lf,平均值为%lf”,油底壳,平均值);
我只是注意到他在早些时候已经有了基本的诀窍。我没有使用它(因为错过了返回而分心),但仍然
感谢他。你只能从一个函数中返回一件东西。但是有几种方法可以解决你的具体问题
首先,您可以声明两个函数,每个函数都有自己的独立任务。这是推荐的设计模式,因为它使您的代码对读者来说更具逻辑性
对于这个双重功能,我将接受三个双重功能,因为这是您设置的方式
double sum(double a, double b, double c) {
return (a + b + c);
}
要从main
调用此函数,您可以说:
double s = sum(n1,n2,n3);
对于你的平均函数,你所要做的就是得到求和函数的结果,然后除以你拥有的元素数,当然在这里是三个
double average(double sum, double n) {
return (sum / n);
}
同样,要从main使用此函数,可以执行以下操作:
double avg = average(s, 3);
int main()
{
double n1, n2, n3;
struct descriptiveStatistics stats;
...
struct descriptiveStatistics analyzeData(double a, double b, double c) {
struct descriptiveStatistics results;
results.sum = a + b + c;
results.avg = sum / 3;
return results;
}
struct descriptiveStatistics* analyzeData(double a, double b, double c) {
struct descriptiveStatistics* results = malloc(sizeof(struct descriptiveStatistics));
// Remember to check for NULL in case there was an error during dynamic memory allocation
if (results == NULL) { /** Handle error ... */ }
results->sum = a + b + c;
results->avg = sum / 3;
return results;
}
int main()
{
struct descriptiveStatistics* stats = analyzeData(n1,n2,n3);
...
理想情况下,您的函数应该更通用,以便更有用,但这是一个非常做作的玩具示例,因此我认为您理解了要点
现在,如果你真的想从一个函数中返回多个值,那么也有一些方法可以做到这一点,尽管同样,推荐的编程实践表明这是一个非常蹩脚的想法,但我认为你仍然应该知道它是如何工作的
您可以声明一个结构:
struct descriptiveStatistics {
double sum;
double avg;
};
您应该记住,为了能够使用它,您必须实例化此结构,我们将在main中这样做:
double avg = average(s, 3);
int main()
{
double n1, n2, n3;
struct descriptiveStatistics stats;
...
struct descriptiveStatistics analyzeData(double a, double b, double c) {
struct descriptiveStatistics results;
results.sum = a + b + c;
results.avg = sum / 3;
return results;
}
struct descriptiveStatistics* analyzeData(double a, double b, double c) {
struct descriptiveStatistics* results = malloc(sizeof(struct descriptiveStatistics));
// Remember to check for NULL in case there was an error during dynamic memory allocation
if (results == NULL) { /** Handle error ... */ }
results->sum = a + b + c;
results->avg = sum / 3;
return results;
}
int main()
{
struct descriptiveStatistics* stats = analyzeData(n1,n2,n3);
...
现在,为了从单个函数中实际返回平均值和和,我们将结合前面的两个函数,如下所示:
double avg = average(s, 3);
int main()
{
double n1, n2, n3;
struct descriptiveStatistics stats;
...
struct descriptiveStatistics analyzeData(double a, double b, double c) {
struct descriptiveStatistics results;
results.sum = a + b + c;
results.avg = sum / 3;
return results;
}
struct descriptiveStatistics* analyzeData(double a, double b, double c) {
struct descriptiveStatistics* results = malloc(sizeof(struct descriptiveStatistics));
// Remember to check for NULL in case there was an error during dynamic memory allocation
if (results == NULL) { /** Handle error ... */ }
results->sum = a + b + c;
results->avg = sum / 3;
return results;
}
int main()
{
struct descriptiveStatistics* stats = analyzeData(n1,n2,n3);
...
在这个基本示例中,为了简单起见,我们只返回一个struct descriptiveStatistics
results结构的副本
double avg = average(s, 3);
int main()
{
double n1, n2, n3;
struct descriptiveStatistics stats;
...
struct descriptiveStatistics analyzeData(double a, double b, double c) {
struct descriptiveStatistics results;
results.sum = a + b + c;
results.avg = sum / 3;
return results;
}
struct descriptiveStatistics* analyzeData(double a, double b, double c) {
struct descriptiveStatistics* results = malloc(sizeof(struct descriptiveStatistics));
// Remember to check for NULL in case there was an error during dynamic memory allocation
if (results == NULL) { /** Handle error ... */ }
results->sum = a + b + c;
results->avg = sum / 3;
return results;
}
int main()
{
struct descriptiveStatistics* stats = analyzeData(n1,n2,n3);
...
然后,从main
创建结构并将其设置为:
double avg = average(s, 3);
int main()
{
double n1, n2, n3;
struct descriptiveStatistics stats;
...
struct descriptiveStatistics analyzeData(double a, double b, double c) {
struct descriptiveStatistics results;
results.sum = a + b + c;
results.avg = sum / 3;
return results;
}
struct descriptiveStatistics* analyzeData(double a, double b, double c) {
struct descriptiveStatistics* results = malloc(sizeof(struct descriptiveStatistics));
// Remember to check for NULL in case there was an error during dynamic memory allocation
if (results == NULL) { /** Handle error ... */ }
results->sum = a + b + c;
results->avg = sum / 3;
return results;
}
int main()
{
struct descriptiveStatistics* stats = analyzeData(n1,n2,n3);
...
我在声明结构时故意不使用typedef
,因为我想一次只处理一件事,但知道不必反复键入这个超长名称可能会有所帮助,这也是一种解脱。您可以在声明中这样做:
typedef struct descriptiveStatistics {
double sum;
double avg;
} *Statistics;
这是我使用typedef
的首选方式。现在,我们不必每次都键入struct descriptiveStatistics*
,现在只需使用Statistics
。请注意,我将其作为struct descriptiveStatistics
的指针,但您也可以执行typedef struct descriptiveStatistics{…}统计;
这将完全相同,只是它显然不是指针。您只能从函数返回一件事。不过,有几种方法可以解决您的特定问题
首先,您可以声明两个函数,每个函数都有自己的独立任务。这是推荐的设计模式,因为它使您的代码对读者来说更具逻辑性
对于这个双重功能,我将接受三个双重功能,因为这是您设置的方式
double sum(double a, double b, double c) {
return (a + b + c);
}
要从main
调用此函数,您可以说:
double s = sum(n1,n2,n3);
对于你的平均函数,你所要做的就是得到求和函数的结果,然后除以你拥有的元素数,当然在这里是三个
double average(double sum, double n) {
return (sum / n);
}
同样,要从main使用此函数,可以执行以下操作:
double avg = average(s, 3);
int main()
{
double n1, n2, n3;
struct descriptiveStatistics stats;
...
struct descriptiveStatistics analyzeData(double a, double b, double c) {
struct descriptiveStatistics results;
results.sum = a + b + c;
results.avg = sum / 3;
return results;
}
struct descriptiveStatistics* analyzeData(double a, double b, double c) {
struct descriptiveStatistics* results = malloc(sizeof(struct descriptiveStatistics));
// Remember to check for NULL in case there was an error during dynamic memory allocation
if (results == NULL) { /** Handle error ... */ }
results->sum = a + b + c;
results->avg = sum / 3;
return results;
}
int main()
{
struct descriptiveStatistics* stats = analyzeData(n1,n2,n3);
...
理想情况下,您的函数应该更通用,以便更有用,但这是一个非常做作的玩具示例,因此我认为您理解了要点
现在,如果您真的非常喜欢从一个函数返回多个值,那么也有一些方法可以做到这一点,尽管同样如此