调用函数时,形参的地址与局部变量地址一样
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct {
int *base;
int *top;
int stackSize;
}SeqStack;
void initial ( SeqStack *S ) {
S->base = ( int * ) malloc ( 10 * sizeof ( int ) );
if ( !S->base ) {
exit ( -1 );
}
S->top = S->base;
S->stackSize = 10;
}
int isEmpty ( SeqStack *S ) {
return S->top == S->base;
}
int isFull ( SeqStack *S ) {
return S->top - S->base == S->stackSize - 1;
}
void push ( SeqStack *S, int data ) {
if ( isFull ( S ) ) {
printf ( "Stack overflow!\n" );
exit ( 1 );
}
*S->top ++ = data;
}
int pop ( SeqStack *S ) {
if ( isEmpty ( S ) ) {
printf ( "Stack is empty!\n" );
}
S->top -= 1;
return *S->top;
}
void conversion ( int N ) {
//void conversion ( int N, int B ) {
int i;
SeqStack *S;
initial ( S );
while ( N ) {
push ( S, N % 8 );
N = N / 8;
//push ( S, N % B );
//N = N / B;
}
while ( !isEmpty ( S ) ) {
i = pop ( S );
printf ( "%d", i );
}
printf ( "\n" );
}
void main ( ) {
int n, d;
printf ( "Input the integer you want to transform:" );
scanf ( "%d", &n );
//printf ( "Input the integer of the system:" );
//scanf ( "%d", &d );
printf ( "Result : " );
//conversion ( n, d );
conversion ( n );
getchar ();
}
当使用注释代码时,conversion 函数被调用时 函数形参 B 与 局部变量 S 地址一样,导致程序出错,求解,c的地址分配规律。
[解决办法]
SeqStack?*S; 这是个空指针
改为
SeqStack?S;
initial ( &S );
[解决办法]
野指针S没有初始化过
SeqStack *S;