gpt4 book ai didi

c - 从 C 中的数组堆栈中弹出 - 将 NULL 分配给 int *

转载 作者:行者123 更新时间:2023-11-30 19:57:47 25 4
gpt4 key购买 nike

考虑以下数组堆栈的定义:

typedef struct larray{
int values[BSize];
struct larray *prox;
}*LArrays;

typedef struct stack{
int sp; //number of occupied positions in stack's top array
LArrays s; //stack top
}Stack;

我写了这个 pop 函数:

int pop(Stack *st, int *t){
if (st->sp == 0) return -1;

if (st->sp == 1){
LArrays newTop = st->s->prox;
free(st->s);
st->s = NULL;
st->s = newTop;
st->sp = BSize;
return 0;
}

LArrays l = st->s;
int *remover = l->values + (--st->sp);
*t = *remover;
*remover = NULL;

return 0;
}

它有效。但是编译时,gcc 会产生以下警告:

warning: assignment makes integer from pointer without a cast [-Wint-conversion]

     *remover = NULL;

将 NULL 赋给 int * 是错误的吗?

[更改为删除器后的结果 = NULL]

主要:

for (; i < 150; i++)
push(stack,i);
i = 0;

printStack(stack);

for (; i < 70; i++)
pop(stack,&r);

printStack(stack);

with *remover = NULL (produces desired output!)

//stack after push

100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|]

---------------------------------------------------------
0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|]

//stack after pop
0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|]

with remover = NULL (does not produce the desired output!)

//stack after push

100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|]

---------------------------------------------------------
0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|]


//stack after pop
0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|]

打印功能:

void printArray(int *v, int N){
int *p = v;
int i = 0;
for(; i < N; i++)
printf("%d|",*p++);
printf("]\n\n");
}

void printStack(Stack *stack){
LArrays p = stack->s;
printf("Free Space on Stack's head: %d\n",BSize - stack->sp);
printf("\nStack:\n\n");
while(p->prox != NULL){
printArray(p->valores,BSize);
printf("------------------------------------------------\n");
p = p->prox;
}printArray(p->values,BSize);
}

最佳答案

您没有将 NULL 分配给 int *。您将其分配给 int

remover 的类型为 int *,而 *remover 取消引用该指针,并且类型为 int

将该行更改为以下内容:

remover = NULL;

实际上,您可以完全删除该行,因为在该点之后不再引用 remover

关于c - 从 C 中的数组堆栈中弹出 - 将 NULL 分配给 int *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com