0

我需要通过参考传递我的课程。我在标题中遇到了错误,但仅限于功能 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;

}
4

2 回答 2

4

这是您的(许多)问题之一。

您声明addTen为:

float addTen (float* number);

并将其称为:

number=addTen(ptr_number); 

这清楚地表明它返回一个值。

现在,您在函数定义中看到 return 语句了吗?

float addTen (float* number)
{
    ptr_number=ptr_number+10;   // No return statement!!
}

接下来,您的所有函数都将 afloat*作为参数,您应该将其读作“指向浮点数的指针”。请注意,指针与值本身不同

要从指向值的指针中获取,您需要“取消引用”它:

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;
}
于 2013-09-20T15:36:20.430 回答
0

因为您没有float从这些函数返回值。如果你想传递一个指向你的函数的指针,那么你应该这样做

float divideByTwelve (float *)
{
    *ptr_number= *ptr_number/12;
     return *ptr_number;
}

但是您已声明number为全局变量,因此无需将指针传递给这些函数。只需传递number自身`;

float divideByTwelve (float number)
{
     return number / 12;
}
于 2013-09-20T15:36:10.633 回答