为什么在执行f_strlen函数的while循环时会出现段错误(只要是循环就会出现段错误)
/*写代码实现strlen、strcat、strcpy、strcmp、strtok
*/
#include <stdio.h>
int i = 0;
char buf[4096];
int f_strlen(int i,char buf[])
{
printf("%d\n",__LINE__);
printf("%d\n",i);
while(buf[i] != '\n')
{
i++;
}
printf("the lenghs of strings is %d\n",i);
}
int main()
{
printf("&××××××××××××××××××××××××××××\n输入0退出\n输入1实现strlen\n输入2实现strcat\n输入3实现strcpy\n输入4实现strcmp\n输入5实现strtok\n*****************\n");
int number;
int flag = 1;
while(1)
{
printf("please input a string!\n");
scanf("%s",buf);
while(flag)
{
printf("please input choose!\n");
scanf("%d",&number);
switch(number)
{
case 0:
flag = 0;
break;
case 1:
//实现strlen
f_strlen(i,buf);
break;
case 2:
/* //实现strcat
printf("输入要比较的字符串\n");
char buf_compare[4096];
scanf("%s",buf_compare);
goto loop:
if(buf[i] == buf_compare[i])
i++;
if*/
break;
case 3:
//实现strcpy
break;
case 4:
//实现strcmp
break;
case 5:
//实现strtok
break;
}
}
}
return 0;
}