程序代码:
#include<stdio.h>
#include<stdlib.h>
extern "C"{
#define STACK_SIZE 10
typedef int DataType;
typedef struct
{
DataType *base;
DataType *top;
int stacksize;
int mark; // check this variable
}SeqStack;
void Initial(SeqStack *s)
{
s->base=(DataType *)malloc(STACK_SIZE*sizeof(DataType));
if(!s->base)exit(-1);
s->top=s->base;
s->mark = -1 ; //
s->stacksize=STACK_SIZE;
}
void Devil_Initial(SeqStack **s )
{
*s= (SeqStack*)malloc(sizeof(SeqStack));
(*s)->base = (DataType *) malloc(STACK_SIZE*sizeof(DataType));
if( !(*s) -> base )
exit( 1 );
(*s)->top = (*s)->base;
(*s)->mark = -2;
(*s)->stacksize=STACK_SIZE;
}
int main()
{
SeqStack *s1=(SeqStack*)malloc(sizeof(SeqStack));// if you do not malloc here , there will be stack error.
Initial( s1 );
printf("%d\n",s1->mark);
SeqStack *s2;// did not malloc here.
Devil_Initial(&s2);
printf("%d\n",s2->mark);
return 0;
}
}
行了BlueGuy,别TMD装清高了。
我是没看到他里面malloc的是一个成员变量,而不是stack本身。
你有多少水我心里清楚,你心里也清楚。