Segmentation fault 错误来了
Segmentation fault Error comes
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n;
void towers(int n,char from, char to,char aux);
clrscr();
printf("\n\t\t PROGRAM FOR TOWERS OF HANOI");
printf("\n\nEnter The Total number of Disks : ");
scanf("%d",n); towers(n,'A','C','B');
getch();
}
void towers(int n,char from,char to, char aux)
{
if(n==1)
{
printf("\nMove disk 1 from %c peg to %c peg",from,to);
return;
}
towers(n-1,from,aux,to);
printf("\n Move disk %d from %c peg to %c peg",n,from,to);
towers(n-1,aux,to,from);
}
将 scanf("%d",n)
替换为 scanf("%d",&n)
,传递指向 scanf
应放置结果的位置的指针。
使用
scanf("%d",&n);
而不是
scanf("%d",n);
因为它需要变量n的地址给scanf函数来存值。
因此,该地址是通过 & n.
指定的
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n;
void towers(int n,char from, char to,char aux);
clrscr();
printf("\n\t\t PROGRAM FOR TOWERS OF HANOI");
printf("\n\nEnter The Total number of Disks : ");
scanf("%d",n); towers(n,'A','C','B');
getch();
}
void towers(int n,char from,char to, char aux)
{
if(n==1)
{
printf("\nMove disk 1 from %c peg to %c peg",from,to);
return;
}
towers(n-1,from,aux,to);
printf("\n Move disk %d from %c peg to %c peg",n,from,to);
towers(n-1,aux,to,from);
}
将 scanf("%d",n)
替换为 scanf("%d",&n)
,传递指向 scanf
应放置结果的位置的指针。
使用
scanf("%d",&n);
而不是
scanf("%d",n);
因为它需要变量n的地址给scanf函数来存值。 因此,该地址是通过 & n.
指定的