【问题标题】:Function is returning 1 instead of a value?函数返回 1 而不是一个值?
【发布时间】:2020-06-16 16:42:55
【问题描述】:

我正在编写一个队列数据结构,一旦值返回到堆栈中,我就无法将整数的值保留在数组中。 pop 函数正在做它需要做的事情,但为什么 main 没有得到这些信息?我错过了什么? malloc?

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

int QUE[20];
const int EMPTY = -1;
int position = -1;
int retrieve = 0;

//push, append to front of array
bool push(int num) {
    if (position >= 20) return false;
    position += 1;
    QUE[position] = num;
    return true;
}

//pop from top of array
bool pop() {

    if(QUE[retrieve] == 0) return false;
    int hold = QUE[retrieve];
    printf("%d",hold);
    retrieve ++;
    return hold;

}

// PEEK

// First in first out

int main() {
    push(12);
    push(90);
    push(22);

    int t;
    //why does pop equal 1
    while ((t = pop()) != 0) {
        printf("t = %d\n",t);

    }

}

【问题讨论】:

  • true 被解释为值 1。
  • 您在push() 中还有一个错误:当position == 19 进入时,您将写入QUE[20],这是超出范围的。而且您在pop() 中对retrieve 进行无限制检查。
  • 我明白了,谢谢。另外,我很困惑为什么这个问题被否决了。有人知道吗?

标签: c boolean queue implicit-conversion


【解决方案1】:

您试图在同一值内传递两种不同类型的信息——布尔状态“弹出成功”和从队列中弹出的整数值。那很糟;并且不匹配导致您将返回类型声明为bool,这导致t 的结果值为零或一(分别作为false 或true 到int 类型的转换)。

尝试将操作拆分为测试和获取阶段,例如:

bool anyItemInQueue()
{
    return _add_appropriate_condition_here_;
}

int main()
{
    ....

    while( anyItemInQueue() )
    {
        int t = pop();

        .... // use t here
    }
}

或传递另一个变量来接收另一个值:

bool pop(int *result)
{
    if( anyItemInQueue() )
    {
        *result = QUE[retrieve];
        ....                        // some housekeeping here
        return true;                // success status
    }
    return false;                   // failure status
}

int main()
{
    ....
    int t;
    while( pop( & t ) )      // point at t to receive the popped value
    {
        .... // use t here
    }
}

【讨论】:

  • 哇,这太有见地了。谢谢!
【解决方案2】:

这是因为任何非零值都被转换为booltrue,然后再转换为整数。 bool的整数值true为1

【讨论】:

    【解决方案3】:

    您的代码具有未定义的行为。

    让我们以函数push为例

    //push, append to front of array
    bool push(int num) {
        if (position >= 20) return false;
        position += 1;
        QUE[position] = num;
        return true;
    }
    

    为了简单起见,我们还假设数组QUE只有一个元素,它被声明为

    int QUE[1];
    

    在这种情况下,由于数组的容量,队列只能包含一个推送值。

    所以在第一次调用push 之后

    push( 0 );
    

    您将拥有 position 等于 0 并且队列包含值 0。

    如果第二次调用该函数,例如

    push( 1 );
    

    函数内的条件

    if (position >= 1) return false;
    

    不会评估为真,因为position 的当前值为0。因此,该函数将尝试将值 1 写入数组 QUE[1] 的无效位置。

    数组只包含一个元素,但该函数允许再写入一个元素。

    现在让我们考虑函数pop

    bool pop() {
    
        if(QUE[retrieve] == 0) return false;
        int hold = QUE[retrieve];
        printf("%d",hold);
        retrieve ++;
        return hold;
    
    }
    

    和同一个队列已经只包含一个等于0的元素(参见上一个调用push( 0 ))。

    作为if语句的条件

    if(QUE[retrieve] == 0) return false;
    

    计算结果为真(队列确实包含提前推入队列的值 0)然后该函数将返回假,就好像队列是空的,尽管它不是空的。

    所以,这个函数是无效的。

    而且在 main 的循环中

    while ((t = pop()) != 0) {
        printf("t = %d\n",t);
    
    }
    

    您似乎正在尝试输出存储在队列中的值。但是,该函数不返回此类值。由于返回类型 bool 是 C 标准类型 _Bool 的 typedef,因此任何返回值都将转换为 0 或 1。

    所以程序完全错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多