在弄进制转换的时候,主函数不加printf就崩溃是怎么回事

六道木_ 2013-12-21 08:36:31
#include<stdio.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量

typedef struct none
{ int *top;
int *base;
int stacksize;
}stack;

void conv(int N,int i);
void CreatStack(stack &s);
void push(stack &s,int e);

void CreatStack(stack &s)//构造一个空栈
{
s.base=(int*)malloc(sizeof(int));
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
}

void push(stack &s,int e)//入栈
{
if(s.top-s.base>=s.stacksize)
{
s.base=(int*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(int));
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++=e;
}

void pop(stack &s,int &e){
if(s.top==s.base) //判断栈是否为空,用e返回出栈的值
printf("11");
else e = *--s.top;

}

void conv(int N,int i)
{
stack s;
int e;
CreatStack(s);
while(N)
{
push(s,N%i);
N=N/i;
}
while(s.top!=s.base)
{
pop(s,e);
printf("%d",e);
}
}

void main()
{
int N;
scanf("%d",&N);
printf("\n");
conv(N,2); //进制转换
}

就是主函数那里,如果去掉printf就崩溃了,把printf放到其他地方也是崩溃,而在printf里随便打什么都可以,但是不打就不行,这printf有什么关系= =
...全文
152 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
六道木_ 2013-12-21
  • 打赏
  • 举报
回复
哦哦,发现了,谢谢啦
引用 3 楼 turingo 的回复:
调试一下指针和栈。 [quote=引用 楼主 u013160932 的回复:] #include<stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACKINCREMENT 10 //存储空间分配增量 typedef struct none { int *top; int *base; int stacksize; }stack; void conv(int N,int i); void CreatStack(stack &s); void push(stack &s,int e); void CreatStack(stack &s)//构造一个空栈 { s.base=(int*)malloc(sizeof(int)); s.top=s.base; s.stacksize=STACK_INIT_SIZE; } void push(stack &s,int e)//入栈 { if(s.top-s.base>=s.stacksize) { s.base=(int*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(int)); s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; } *s.top++=e; } void pop(stack &s,int &e){ if(s.top==s.base) //判断栈是否为空,用e返回出栈的值 printf("11"); else e = *--s.top; } void conv(int N,int i) { stack s; int e; CreatStack(s); while(N) { push(s,N%i); N=N/i; } while(s.top!=s.base) { pop(s,e); printf("%d",e); } } void main() { int N; scanf("%d",&N); printf("\n"); conv(N,2); //进制转换 } 就是主函数那里,如果去掉printf就崩溃了,把printf放到其他地方也是崩溃,而在printf里随便打什么都可以,但是不打就不行,这printf有什么关系= =
[/quote]
图灵狗 2013-12-21
  • 打赏
  • 举报
回复
单补调试看看指针操作是否越界。
引用 4 楼 u013160932 的回复:
[quote=引用 3 楼 turingo 的回复:] 调试一下指针和栈。 [quote=引用 楼主 u013160932 的回复:] #include<stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACKINCREMENT 10 //存储空间分配增量 typedef struct none { int *top; int *base; int stacksize; }stack; void conv(int N,int i); void CreatStack(stack &s); void push(stack &s,int e); void CreatStack(stack &s)//构造一个空栈 { s.base=(int*)malloc(sizeof(int)); s.top=s.base; s.stacksize=STACK_INIT_SIZE; } void push(stack &s,int e)//入栈 { if(s.top-s.base>=s.stacksize) { s.base=(int*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(int)); s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; } *s.top++=e; } void pop(stack &s,int &e){ if(s.top==s.base) //判断栈是否为空,用e返回出栈的值 printf("11"); else e = *--s.top; } void conv(int N,int i) { stack s; int e; CreatStack(s); while(N) { push(s,N%i); N=N/i; } while(s.top!=s.base) { pop(s,e); printf("%d",e); } } void main() { int N; scanf("%d",&N); printf("\n"); conv(N,2); //进制转换 } 就是主函数那里,如果去掉printf就崩溃了,把printf放到其他地方也是崩溃,而在printf里随便打什么都可以,但是不打就不行,这printf有什么关系= =
[/quote]怎么调= =[/quote]
六道木_ 2013-12-21
  • 打赏
  • 举报
回复
引用 3 楼 turingo 的回复:
调试一下指针和栈。 [quote=引用 楼主 u013160932 的回复:] #include<stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACKINCREMENT 10 //存储空间分配增量 typedef struct none { int *top; int *base; int stacksize; }stack; void conv(int N,int i); void CreatStack(stack &s); void push(stack &s,int e); void CreatStack(stack &s)//构造一个空栈 { s.base=(int*)malloc(sizeof(int)); s.top=s.base; s.stacksize=STACK_INIT_SIZE; } void push(stack &s,int e)//入栈 { if(s.top-s.base>=s.stacksize) { s.base=(int*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(int)); s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; } *s.top++=e; } void pop(stack &s,int &e){ if(s.top==s.base) //判断栈是否为空,用e返回出栈的值 printf("11"); else e = *--s.top; } void conv(int N,int i) { stack s; int e; CreatStack(s); while(N) { push(s,N%i); N=N/i; } while(s.top!=s.base) { pop(s,e); printf("%d",e); } } void main() { int N; scanf("%d",&N); printf("\n"); conv(N,2); //进制转换 } 就是主函数那里,如果去掉printf就崩溃了,把printf放到其他地方也是崩溃,而在printf里随便打什么都可以,但是不打就不行,这printf有什么关系= =
[/quote]怎么调= =
图灵狗 2013-12-21
  • 打赏
  • 举报
回复
调试一下指针和栈。
引用 楼主 u013160932 的回复:
#include<stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACKINCREMENT 10 //存储空间分配增量 typedef struct none { int *top; int *base; int stacksize; }stack; void conv(int N,int i); void CreatStack(stack &s); void push(stack &s,int e); void CreatStack(stack &s)//构造一个空栈 { s.base=(int*)malloc(sizeof(int)); s.top=s.base; s.stacksize=STACK_INIT_SIZE; } void push(stack &s,int e)//入栈 { if(s.top-s.base>=s.stacksize) { s.base=(int*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(int)); s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; } *s.top++=e; } void pop(stack &s,int &e){ if(s.top==s.base) //判断栈是否为空,用e返回出栈的值 printf("11"); else e = *--s.top; } void conv(int N,int i) { stack s; int e; CreatStack(s); while(N) { push(s,N%i); N=N/i; } while(s.top!=s.base) { pop(s,e); printf("%d",e); } } void main() { int N; scanf("%d",&N); printf("\n"); conv(N,2); //进制转换 } 就是主函数那里,如果去掉printf就崩溃了,把printf放到其他地方也是崩溃,而在printf里随便打什么都可以,但是不打就不行,这printf有什么关系= =
六道木_ 2013-12-21
  • 打赏
  • 举报
回复
引用 1 楼 KenZhang1031 的回复:
崩溃和printf没有关系,你还是找找其它原因。
可是我把printf去掉后就崩溃了= = 这是怎么回事
Mr. Code 2013-12-21
  • 打赏
  • 举报
回复
崩溃和printf没有关系,你还是找找其它原因。

69,378

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧