读书人

指针释放和结构声明有关问题

发布时间: 2012-02-05 12:07:15 作者: rapoo

指针释放和结构声明问题
#include <stdio.h>
#include <stdlib.h>
#define MAX_INT_SIZE 10
#define INCREAMENT 2
#define OK 1
#define OVERFLOW -2
typedef int status;

typedef struct{
char inf;
int no;
int time;
}Event;
typedef struct{
Event *top;
Event *base;
int stacksize;
}SqStack;

status InitStack(SqStack *S){
/*初始化栈*/
S-> base=(Event *)malloc(MAX_INT_SIZE*sizeof(Event));
if(!(S-> base)) return OVERFLOW;
S-> top=S-> base;
S-> stacksize=MAX_INT_SIZE;
return OK;
}/*InitStack*/
Status DestroyStack(SqStack *S){
/*销毁栈S,并返回OK*/
free(S);
return OK;
}/*DestroyStack*/

void main(){
printf( "Enter the information of the car! ");
Event *pa;
scanf( "%c%d%d ",&(pa-> inf),&(pa-> no),&(pa-> time));
}
--------------主要问题-------------
DestroyStack函数
main函数中pa未定义


------------------报这样的错-----------------------

C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(28) : error C2061: syntax error : identifier 'DestroyStack '
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(28) : error C2059: syntax error : '; '
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(28) : error C2059: syntax error : 'type '
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(36) : error C2275: 'Event ' : illegal use of this type as an expression
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(13) : see declaration of 'Event '
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(36) : error C2065: 'pa ' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(37) : error C2223: left of '-> inf ' must point to struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(37) : error C2223: left of '-> no ' must point to struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TEST.C(37) : error C2223: left of '-> time ' must point to struct/union
Error executing cl.exe.

TEST.OBJ - 8 error(s), 0 warning(s)



[解决办法]
Status
========
status
[解决办法]
pa要先动态分配一块内存才可以用的
[解决办法]
pa = (Event *)malloc(1*sizeof(Event));
------解决方案--------------------


#include <stdio.h>
#include <stdlib.h>
#define MAX_INT_SIZE 10
#define INCREAMENT 2
#define OK 1
#define OVERFLOW -2
typedef int status;

typedef struct{
char inf;
int no;
int time;
}Event;
typedef struct{
Event *top;
Event *base;
int stacksize;
}SqStack;

status InitStack(SqStack *S){
/*初始化栈*/
S-> base=(Event *)malloc(MAX_INT_SIZE*sizeof(Event));
if(!(S-> base)) return OVERFLOW;
S-> top=S-> base;
S-> stacksize=MAX_INT_SIZE;
return OK;
}/*InitStack*/

int DestroyStack(SqStack *S){
/*销毁栈S,并返回OK*/
free(S);
return OK;
}/*DestroyStack*/

void main(){
printf( "Enter the information of the car! ");
Event *pa = (Event *)malloc(sizeof(Event));
scanf( "%c%d%d ",&(pa-> inf),&(pa-> no),&(pa-> time));
}
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#define MAX_INT_SIZE 10
#define INCREAMENT 2
#define OK 1
#define OVERFLOW -2
typedef int status;

typedef struct{
char inf;
int no;
int time;
}Event;
typedef struct{
Event *top;
Event *base;
int stacksize;
}SqStack;

status InitStack(SqStack *S){
/*初始化栈*/
S-> base=(Event *)malloc(MAX_INT_SIZE*sizeof(Event));
if(!(S-> base)) return OVERFLOW;
S-> top=S-> base;
S-> stacksize=MAX_INT_SIZE;
return OK;
}/*InitStack*/

int DestroyStack(SqStack *S){
/*销毁栈S,并返回OK*/
free(S);
return OK;
}/*DestroyStack*/

void main(){
printf( "Enter the information of the car! ");
Event *pa = (Event *)malloc(sizeof(Event));
scanf( "%c%d%d ",&(pa-> inf),&(pa-> no),&(pa-> time));
}
==================================================

1.此程序在TC2.0中无法通过,提示pa没有定义。

2. 在dev-c++中 void main()更改 为int main()才能编译通过,不知道为什么会出现这样的错误。
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#define MAX_INT_SIZE 10
#define INCREAMENT 2
#define OK 1
#define OVERFLOW -2
typedef int status;

typedef struct{
char inf;
int no;
int time;
}Event;
typedef struct{
Event *top;
Event *base;
int stacksize;
}SqStack;

status InitStack(SqStack *S){
/*初始化栈*/
S-> base=(Event *)malloc(MAX_INT_SIZE*sizeof(Event));
if(!(S-> base)) return OVERFLOW;
S-> top=S-> base;
S-> stacksize=MAX_INT_SIZE;
return OK;
}/*InitStack*/

status DestroyStack(SqStack *S){
/*销毁栈S,并返回OK*/
free(S);
return 1;
}/*DestroyStack*/

void main(){
printf( "Enter the information of the car! ");
Event *pa = (Event *)malloc(sizeof(Event));
scanf( "%c%d%d ",&(pa-> inf),&(pa-> no),&(pa-> time));
}


---------------------------------------
不知道你这个程序时干嘛用的

随便改了下

但能通过编译了

你看看有帮助不

读书人网 >C语言

热点推荐