这是一个关于字符串操作的简单程序,但我在运行时遇到了段错误(核心转储)。
我正在尝试在最新的 ubuntu 版本的终端中运行该程序。 这是代码片段...
#include<stdio.h>
void main()
{
char s1[50],s2[50];
int ch,i,j,l,l1,l2,flag,count1=0,count2=0;
do
{
printf("\n MENU \n");
printf("\n 1.String concatenation \n");
printf("\n 2.String comparison \n");
printf("\n 3.Substring \n");
printf("\n Enter a choice ...");
scanf("%d",ch);
switch(ch)
{
case 1:
printf("\n Enter the first string \n");
gets(s1);
printf("\n Enter the second string \n");
gets(s2);
for(int i=0;s1[i]!='\0';i++)
代码一直有效,直到显示菜单部分,但之后显示段错误(核心转储)。
最佳答案
scanf("%d",ch); --> scanf("%d",&ch);
scanf
获取要存储值的变量地址。
它指定,
d Matches an optionally signed decimal integer; the next pointer must be a pointer to int.
关于c - 如何使用 gdb 调试带有核心转储的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639949/