主题:请问char *train和char train[]在这里有何不同?
严版 数据结构 习题集 第24页3.16火车调度问题,下面是我写的完整的程序,就红字部分向大家请教,如果我的程序哪里写得不够规范,简洁,也请大家指教,谢谢。
#include <stdlib.h>
#include <stdio.h>
#define ERROR 0
#define OK 1
#define INFEASIBLE -1
#define NULL 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 3
#define STACKINCREMENT 3
typedef char SElemType;
typedef int Status;
typedef struct{ //定义
SElemType *base,*top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){
if(!(S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)))) return OVERFLOW;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,char x){
if(S.top-S.base>=S.stacksize){
if(!(S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType)))) return OVERFLOW;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=x;
return OK;
}
Status Pop(SqStack &S,char &x){
if(S.top==S.base) return ERROR;
else x=*(--S.top);
return OK;
}
Status Done(SqStack &S){
char train[]="HSHSHSSHSH"; //为何此处将train[]改为*train,会在语句[2]处出错?
char *p,*q;
char temp;
int i=1;
q=p=train;
while(*p!='\0'){
if(*p=='H'){
Push(S,*p);
printf("第%d节车厢进栈\n",i);
}
else{
printf("第%d节车厢进栈、出栈\n",i);
*(q++)=*p; [color=800000]//[2]为何将train[]改为*train,会在这里出错?[/color] } [color=800000]//此时p指向第2个字符S,而q指向第一个字符H[/color] i++;p++;
}
while(Pop(S,temp)) *(q++)=temp;
printf("%s\n",train);
return OK;
}
void main(){
SqStack S;
InitStack(S);
Done(S);
}
#include <stdlib.h>
#include <stdio.h>
#define ERROR 0
#define OK 1
#define INFEASIBLE -1
#define NULL 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 3
#define STACKINCREMENT 3
typedef char SElemType;
typedef int Status;
typedef struct{ //定义
SElemType *base,*top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){
if(!(S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)))) return OVERFLOW;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,char x){
if(S.top-S.base>=S.stacksize){
if(!(S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType)))) return OVERFLOW;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=x;
return OK;
}
Status Pop(SqStack &S,char &x){
if(S.top==S.base) return ERROR;
else x=*(--S.top);
return OK;
}
Status Done(SqStack &S){
char train[]="HSHSHSSHSH"; //为何此处将train[]改为*train,会在语句[2]处出错?
char *p,*q;
char temp;
int i=1;
q=p=train;
while(*p!='\0'){
if(*p=='H'){
Push(S,*p);
printf("第%d节车厢进栈\n",i);
}
else{
printf("第%d节车厢进栈、出栈\n",i);
*(q++)=*p; [color=800000]//[2]为何将train[]改为*train,会在这里出错?[/color] } [color=800000]//此时p指向第2个字符S,而q指向第一个字符H[/color] i++;p++;
}
while(Pop(S,temp)) *(q++)=temp;
printf("%s\n",train);
return OK;
}
void main(){
SqStack S;
InitStack(S);
Done(S);
}