簡體   English   中英

將結構指針存儲在c中的堆棧

[英]stack storing struct pointers in c

該程序應該實現用於存儲和檢索結構指針的堆棧。 該結構包含一個int和兩個結構變量。 推功能工作正常,但是當我彈出結構指針並嘗試訪問其中的數據時,會出現執行錯誤。

#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;
}
if( top = -1)

應該

if( top == -1 )

使用=您將-1分配給top 要檢查是否相等,請使用==

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM