我需要通过参考传递我的课程。我在标题中收到错误,但仅限于函数divideByTweleve和multiplyByEleven。这是为什么?我究竟做错了什么? 请帮我解决这个问题。
#include <stdio.h>
void displayMenu();
float addTen (float* number);
float divideByTwelve(float* number);
float subtractSixteen(float* number);
float multiplyByEleven(float* number);
int menu;
float number, finalNumber;
float* ptr_number;
int main (void)
{
float* ptr_number=NULL;
ptr_number=&number;
printf("Please enter a number : ");
scanf("%f", &number);
displayMenu();
scanf("%d", &menu);
do {
printf("\nEnter 5 to see final number\n");
scanf("%d",&menu);
switch (menu)
{
case 1: number=addTen(ptr_number);
break;
case 2: number=divideByTwelve(ptr_number);
break;
case 3: number=subtractSixteen(ptr_number);
break;
case 4: number=multiplyByEleven(ptr_number);
break;
}
printf("Your number is: %.2f \n", number);
}while(menu!=5);//close of the do-while loop
}
void displayMenu()
{
printf("How would you like to manipulate your number\n");
printf("1. Add 10\n");
printf("2. Divide by 12\n");
printf("3. Subtract 16\n");
printf("4. Multiply by 11\n");
}
float addTen (float* number)
{
ptr_number=ptr_number+10;
}
float divideByTwelve (float* number)
{
ptr_number=ptr_number/12;
}
float subtractSixteen(float* number)
{
ptr_number=ptr_number-16;
}
float multiplyByEleven(float* number)
{
ptr_number=ptr_number*11;
}
答案 0 :(得分:4)
这是你的(很多)问题之一。
您将addTen
声明为:
float addTen (float* number);
并将其命名为:
number=addTen(ptr_number);
清楚地表明它返回一个值。
现在,你在函数定义中看到了一个return语句吗?
float addTen (float* number)
{
ptr_number=ptr_number+10; // No return statement!!
}
接下来,您的所有函数都将float*
作为参数,您应将其读作“指向浮点数的指针”。请注意,指针 不 与值本身相同。
要从指针获取值,您需要“取消引用”它:
float addTen (float* number)
{
*ptr_number = *ptr_number + 10;
}
每个*
之前的星号(ptr_number
)表示从指针返回实际值。
之前,你试图在指针上添加10 (这是有效的,但在当前情况下不合适)
现在,使用*
,您将向存储在指向位置的值添加10。
最终,这是我更正的代码版本
void displayMenu();
void addTen (float* number);
void divideByTwelve(float* number);
void subtractSixteen(float* number);
void multiplyByEleven(float* number);
int main (void)
{
int menu;
float number;
printf("Please enter a number : ");
scanf("%f", &number);
displayMenu();
scanf("%d", &menu);
do {
printf("\nEnter 5 to see final number\n");
scanf("%d",&menu);
switch (menu)
{
case 1: addTen(&number); break;
case 2: divideByTwelve(&number); break;
case 3: subtractSixteen(&number); break;
case 4: multiplyByEleven(&number); break;
}
printf("Your number is: %.2f \n", number);
}while(menu!=5);//close of the do-while loop
printf("Your FINAL number is: %.2f \n", number);
}
void displayMenu()
{
printf("How would you like to manipulate your number\n");
printf("1. Add 10\n");
printf("2. Divide by 12\n");
printf("3. Subtract 16\n");
printf("4. Multiply by 11\n");
}
void addTen (float* ptr_number)
{
*ptr_number = *ptr_number+10;
}
void divideByTwelve (float* ptr_number)
{
*ptr_number = *ptr_number/12;
}
void subtractSixteen(float* ptr_number)
{
*ptr_number = *ptr_number-16;
}
void multiplyByEleven(float* ptr_number)
{
*ptr_number = *ptr_number*11;
}
答案 1 :(得分:0)
因为您没有从这些函数返回float
值。如果你想传递指向你的函数的指针,你应该这样做
float divideByTwelve (float *)
{
*ptr_number= *ptr_number/12;
return *ptr_number;
}
但是您已将number
声明为全局变量,因此无需传递指向这些函数的指针。只需通过number
本身`;
float divideByTwelve (float number)
{
return number / 12;
}