我想从main调用所有三个函数 并在主要的两个函数定义中打印结果 我无法找到如何做到这一点我已经创建了我需要的所有功能和指针。
#include <math.h>
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS //to avoid scanf warning or error
int CalculateSum(int num1, int num2);
int CalculateProduct(int num1, int num2);
void CalculateBoth(int num1, int num2, int*sumPtr, int *prodPtr);
int main(void)
{
}
int CalculateSum(int num1, int num2)
{
int temp1;
num1 = 1;
num2 = 2;
temp1 = num1 + num2;
//printf("calculatesum value %d ", temp1);
return (temp1);
}
int CalculateProduct(int num1, int num2)
{
num1 = 3;
num2 = 4;
int temp2;
temp2 = num1*num2;
printf("calc product %d ", temp2);
return temp2;
}
void CalculateBoth(int num1, int num2, int*sumPtr, int *prodPtr)
{
num1 = 5;
num2 = 6;
int temp3 = num1 + num2;
sumPtr = &temp3;
int temp4 = num1*num2;
prodPtr = temp4;
printf("calc sumptr %d ", temp3);
printf("Calc both prodptr %d ", temp4);
}
答案 0 :(得分:1)
嗯,显然你需要一些帮助。你的功能并不像你期望的那样。对于初学者,您将num1
和num2
传递给函数。为了实现这一点,必须在main中声明它们。除了num1
和num2
之外,您还需要声明变量来保存前两个函数的返回值,比如sum
和prod
。这些变量也将作为指向CalculateBoth函数的指针传递,以便它们的值在main()
中更新并可用。您只需要main()
中的简单定义:
/* declare and initialize variables */
int n1 = 0, n2 = 0, sum = 0, prod = 0;
虽然更改函数中变量的值没有任何问题,但是由于您编写了所有函数,因此完全无法将num1
和num2
传递给函数。看起来您打算将num1
和num2
的值传递给每个函数,而不是更改函数中的值。例如,您希望在main()
中看到:
/* CalculateSum */
n1 = 1, n2 = 2;
sum = CalculateSum (n1, n2);
/* CalculateProduct */
n1 = 3, n2 = 4;
prod = CalculateProduct (n1, n2);
/* CalculateBoth (passing pointer to have value back in main()) */
n1 = 5, n2 = 6;
CalculateBoth (n1, n2, &sum, &prod);
您的任务的剩余部分是在函数中打印值(sum
,prod
或both
),然后再次在main()
中为您投保已成功更新函数范围内的值以及main()
(函数本身)。下面显示了在函数和main中简单添加相同的print语句。如果您有疑问,请告诉我。
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS // I'll take your word for it...
int CalculateSum (int num1, int num2);
int CalculateProduct (int num1, int num2);
void CalculateBoth (int num1, int num2, int *sumPtr, int *prodPtr);
int main(void)
{
/* declare and initialize variables */
int n1 = 0, n2 = 0, sum = 0, prod = 0;
/* CalculateSum */
n1 = 1, n2 = 2;
sum = CalculateSum (n1, n2);
printf (" calculatesum value %d\n", sum);
/* CalculateProduct */
n1 = 3, n2 = 4;
prod = CalculateProduct (n1, n2);
printf (" calc product %d\n", prod);
/* CalculateBoth (passing pointer to have value back in main()) */
n1 = 5, n2 = 6;
CalculateBoth (n1, n2, &sum, &prod);
printf(" calc both sumPtr %d\n", sum);
printf(" calc both prodPtr %d\n", prod);
return 0;
}
int CalculateSum (int num1, int num2)
{
int sum;
sum = num1 + num2;
printf ("\n calculatesum value %d\n", sum);
return sum;
}
int CalculateProduct (int num1, int num2)
{
int prod;
prod = num1 * num2;
printf ("\n calc product %d\n", prod);
return prod;
}
void CalculateBoth (int num1, int num2, int *sumPtr, int *prodPtr)
{
int sum = num1 + num2;
*sumPtr = sum;
int prod = num1 * num2;
*prodPtr = prod;
printf("\n calc both sumPtr %d\n", sum);
printf(" calc both prodPtr %d\n", prod);
}
<强>输出强>
如果仔细查看代码,您会看到在函数和main中都打印了同样的东西:
$ ./bin/fncinmain
calculatesum value 3
calculatesum value 3
calc product 12
calc product 12
calc both sumPtr 11
calc both prodPtr 30
calc both sumPtr 11
calc both prodPtr 30
答案 1 :(得分:0)
#include <math.h>
#include <stdio.h>
#include <conio.h>
#define _CRT_SECURE_NO_WARNINGS //to avoid scanf warning or error
int CalculateSum(int num1, int num2);
int CalculateProduct(int num1, int num2);
void CalculateBoth(int num1, int num2, int*sumPtr, int *prodPtr);
int main(void) {
int x = 5, y = 6, prod, sum;
sum = CalculateSum(x, y);
prod = CalulateProduct(x, y);
printf("sum=%d Product=%d", x, y);
printf("now calulate both at once");
x = 4;
y = 3;
CalculateBoth(x, y, &sum, &prod);
printf("%d %d", sum, prod);
getch();
return 0;
}
int CalculateSum(int num1, int num2) {
int temp1;
temp1 = num1 + num2;
printf("calculatesum value %d ", temp1);
return (temp1);
}
int CalculateProduct(int num1, int num2) {
int temp2;
temp2 = num1*num2;
printf("calc product %d ", temp2);
return temp2;
}
void CalculateBoth(int num1, int num2, int* sumPtr, int *prodPtr) {
int temp1, temp2;
temp1 = num1 + num2;
*sumPtr = temp1;
temp2 = num1*num2;
*prodPtr = temp2;
printf("calc sumptr %d ", temp1);
printf("Calc both prodptr %d ", temp2);
}
答案 2 :(得分:-1)
如果要返回多个值,请从函数返回一个数组。
int * CalculateBoth(int num1, int num2, int*sumPtr, int *prodPtr)
{
num1 = 5;
num2 = 6;
int arr[2];
arr[0] = num1 + num2;
sumPtr = &temp3;
arr[1] = num1*num2;
prodPtr = temp4;
printf("calc sumptr %d ", temp3);
printf("Calc both prodptr %d ", temp4);
return arr;
}
然后在主
int main()
{
int *p;
p=CalculateBoth(...);
printf("%d %d",p[0],p[1]);
return 0;
}