哪位C语言的高手帮看一下

来源:百度知道 编辑:UC知道 时间:2024/05/18 21:18:11
下面这段C语言的程序用VC来编译的时候为什么会出现这个问题
:unhandled exception in 1.exe:0xC0000005:Access Violation.

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char * base;
char * top;
int stacksize;
}STACK;
void Initstack(STACK *s)
{
s -> base = (char*) malloc (100* sizeof(char)) ;
s ->top=s ->base;
s ->stacksize=100;
}
void main()
{
int m;
STACK *OPTR;
scanf("%d",&m);
Initstack (OPTR);
}

STACK *OPTR; Initstack (OPTR);
定义有问题,应该定义为STACK OPRT,Initstack(&OPTR).
你的定义了一个指针,而Initstack并没有为指针赋值的操作,空指针导致未知错误

使用没有分配内存的指针,STACK *OPTR;
OPTR使用之前必须分配内存;
修改如下:
typedef struct myStack
{
char * base;
char * top;
int stacksize;
}STACK;
STACK *OPTR = new struct myStack;