Sai*_*adi 1 c arrays struct pointers
该程序应该实现用于存储和检索struct指针的堆栈.该struct包含一个int和两个struct变量.推送功能工作正常,但是当我弹出结构指针并尝试访问其中的数据时,会出现执行错误.
#include<stdio.h>
#include<malloc.h>
#define MAX 10
struct node *arr[MAX];
int top=-1;
struct node* pop(){
if(top=-1)
return NULL;
else{
return arr[top--];
}
}
void push(struct node* d){
if(top==MAX-1)
return;
else{
arr[++top]=d;
}
}
int main(){
struct node* t = (struct node*)malloc(sizeof(struct node));
t->data=9;
push(t);
printf("%d",pop()->data);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
if( top = -1)
Run Code Online (Sandbox Code Playgroud)
应该
if( top == -1 )
Run Code Online (Sandbox Code Playgroud)
随着=你分配-1到top.要检查是否相等,请使用==.
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |