函数返回 1 而不是值?
Function is returning 1 instead of a value?
我正在编写一个队列数据结构,一旦值返回到堆栈中,我就无法在数组中保留整数值。 pop 函数正在做它需要做的事情,但为什么 main 没有得到该信息?我错过了什么?分配?
#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);
}
}
这是因为任何非零值都被转换为 bool
true 然后再转换为整数。 bool
true的整数值为1
您正试图在同一值内传递两种不同类型的信息——布尔状态 'the pop succeeded' 和从队列中弹出的整数值。那很糟;不匹配导致您将 return 类型声明为 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
}
}
您的代码有未定义的行为。
让我们以函数 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)然后函数将 return 假,就好像队列是空的,尽管它不是空的。
所以,这个功能是无效的。
而且在主循环中
while ((t = pop()) != 0) {
printf("t = %d\n",t);
}
您似乎正在尝试输出存储在队列中的值。但是函数没有 return 这样的值。由于 return 类型 bool
是 C 标准类型 _Bool
的类型定义,任何 returned 值都被转换为 0
或 1
.
所以程序完全错误。
我正在编写一个队列数据结构,一旦值返回到堆栈中,我就无法在数组中保留整数值。 pop 函数正在做它需要做的事情,但为什么 main 没有得到该信息?我错过了什么?分配?
#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);
}
}
这是因为任何非零值都被转换为 bool
true 然后再转换为整数。 bool
true的整数值为1
您正试图在同一值内传递两种不同类型的信息——布尔状态 'the pop succeeded' 和从队列中弹出的整数值。那很糟;不匹配导致您将 return 类型声明为 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
}
}
您的代码有未定义的行为。
让我们以函数 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)然后函数将 return 假,就好像队列是空的,尽管它不是空的。
所以,这个功能是无效的。
而且在主循环中
while ((t = pop()) != 0) {
printf("t = %d\n",t);
}
您似乎正在尝试输出存储在队列中的值。但是函数没有 return 这样的值。由于 return 类型 bool
是 C 标准类型 _Bool
的类型定义,任何 returned 值都被转换为 0
或 1
.
所以程序完全错误。