【问题标题】:returned float pointer not storing data permanently返回的浮点指针未永久存储数据
【发布时间】:2015-06-26 16:45:23
【问题描述】:

我正在用 C 语言编写一个简单的程序,以将浮点数输出为 int 和十六进制等。

对于这个作业,我不能更改 main 中的任何内容。

我有一个函数,getNextFloat(&f),它获取“float f;”的地址调用 scanf 来获取浮点值,然后返回一个指向 f 的新值的指针,该指针被发送到我的打印函数(它将浮点值转换为十六进制和其他表示形式)。

我的问题是,当我运行程序时,当我在函数 getNextFloat 中调用 scanf 并输入一个数字时,如果我立即在 getNextFloat 函数中打印 *f 它打印得很好,但是如果我返回 *f 并打印 'f ' 在 print 函数中,无论我在 getNextFloat 中输入什么数字,它的值都是 0。我不确定为什么'f'的值没有被保存并且似乎是getNextFloat的本地。

#include <stdio.h>
#include <stdlib.h>

static char *studentName = "me";

// report whether machine is big or small endian:

void bigOrSmallEndian()
{
        // irrelevant to question; contains code to report 'endian-ness'
}

// note: the following 3 comments are instructions from the teacher
// get next float using scanf()
// returns 1 (success) or 0 (failure)
// if call succeeded, return float value via f pointer:

int getNextFloat(float *f)
{
        float fl;

        scanf("%f", &fl);
        f = &fl;
        printf("%f", *f);
        return *f;
}

void printNumberData(float f)
{
        // note: function is incomplete, trying to fix this pointer thing first

        printf("%10f", f);
        printf("%10x\n", f);
}

// do not change this function in any way
int main(int argc, char **argv)
{
        float   f;
        int     nValues;

        printf("CS201 - A01 - %s\n\n", studentName);
        bigOrSmallEndian();
        for (;;) {
                if(argc == 1)
                        printf("> ");
                nValues = getNextFloat(&f);
                if(! nValues) {
                        printf("bad input\n");
                        while (getchar() != '\n');
                        continue;
                        }
                printNumberData(f);
                if(f == 0.0)
                        break;
                }
        printf("\n");
        return 0;
}

作为一个例子,这是我运行代码时得到的结果:

byte order: little-endian

> 9
9.000000  0.000000  7ffffff5

它打印的第一个值 9.000000 是我在 getNextFloat 函数中放入的对 printf 的测试程序调用,以表明它正在正确扫描并且问题出在其他地方。

接下来的两个值显然是调用 print 函数时存储在 f 中的值。

感谢您的见解

【问题讨论】:

  • 重新检查getNextFloat() - 它没有按照您的想法执行...
  • 根据 cmets 中的说明,getNextFloat(float*f) 可以简单地为return scanf("%f", f) == 1;(上述功能的用处留给想象)。
  • 哦,哼哼。我现在看到了。我不确定如何处理 0 或 1 指令。非常感谢你们俩

标签: c pointers


【解决方案1】:

让我们来看看getNextFloat发生了什么

int getNextFloat( float *f ) {
        float fl; // declare a stack variable named fl, containing junk at this point
        scanf( "%f", &fl ); // read a value into fl
        f = &fl; // f now points to fl, which is on the stack
        return *f; // return the value of fl truncated to an integer
} // pop fl from the stack, the next thing to be pushed onto the stack will overwrite the contents of what f was pointing to

请记住,从函数内部对f 作为值的任何修改都不会修改函数外部的任何值。此时 f 实际上是一个局部变量,除非您取消引用它并写入f 指向的内存(应该是main 中名为f 的变量)

您要做的是将f = &amp;fl; 行更改为*f = fl;

【讨论】:

    【解决方案2】:

    不要使用制表符进行缩进,因为每个编辑器 制表位/制表位宽度设置不同。 IE。使用(比如说)4 个空格来提高缩进的可读性。

    这一行,在getNextFloat()中

    f = &fl;
    

    是向后的。

    应该是:

    *f = fl;
    

    记住任务是如果scanf成功返回1,否则返回0

    整体功能应该是:

    int getNextFloat(float *f)
    {
            float fl;
            int success = 0;
    
            success = scanf("%f", &fl);
    
            // always place literal on left
            // so an error like '=' will be caught by the compiler
            if(1 == success )
            { // then scanf successful
                *f = fl;
                //printf("%f", *f);
            }
            else
            { // else, overwrite possible EOF value in success
                success = 0;
            }
            return success;
    } // end function: getNextFloat
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多