當我試圖將元素推送到堆棧我得到分段錯誤,但如果我打開堆棧地址(我標記爲「!!!」),它是它接受它的符號。但是這次在每次推動中,它會創建新地址並且不會增加最高價值。c指針作爲輸入
typedef struct
{
struct table **symbols; // array of the stack
int top; //index of the top element
int size; //maximum size of the stack
}stack;
void push(stack *stck,struct table *element)
{
if(stck->top == stck->size)
{
printf("stack is full");
return;
}
stck = malloc(sizeof(stack)); !!!
stck->symbols = (struct table **)malloc(50 * sizeof(struct table*)); !!!
printf("top : %d\n",stck->top);
stck->top = stck->top++;
printf("%d"&stck->top);
stck->symbols[stck->top] = element;
printf("top : %d\n",stck->top);
}
你所有的邏輯是完全錯誤的。 投射不會使指向指針的指針確實指向指針的地址。 你必須用一個循環手動填充它,併爲'表'的每一行分配空間 – 2009-12-21 16:55:06