Simon_tj 2022-03-09 17:20 采纳率: 16.7%
浏览 36

C语言出现Segmentationfault错误

#include<stdio.h>
#include<string.h>

struct net_info
{
int type;
char usr_name[20];
char usr_pwd[20];
char file_name[100];
char file_text[500];
};

struct net_info usr_msg;

int main()
{
printf("Menu Navigation :\n");
printf("1.Logon;\n2.Login;\n3.List;\n4.Get;\n5.Put;\n6.Exit;\n");
printf("Please enter your choice : ");
int choice;
scanf("%d",&choice);
usr_msg.type = choice;
if(choice == 1)
{
printf("Create an user-name : \n");
while(1)
{
char *name_buf = NULL;
gets(name_buf);
strcpy(usr_msg.usr_name,name_buf);
int i = 0,j;
while(usr_msg.usr_name[i] != '\0')
{
i++;
}
for(j = 0;usr_msg.usr_name[j] != '\0';j++)
{
if(usr_msg.usr_name[j] == ' ')
break;
}
if(i > 20 || j < i)
{
printf("The user name can be up to 20 characters and not be any white space\n");
printf("Re-enter : \n");
}
else
{
printf("User-name set successfully!\n");
break;
}
}
printf("Create an user-password(include numbers,letters) : \n");
while(1)
{
char *password_buf = NULL;
int i = 0,j,number = 0,letter = 0;
gets(password_buf);
strcpy(usr_msg.usr_name,password_buf);
while(usr_msg.usr_pwd[i] != '\0')
{
i++;
}
for(j = 0;usr_msg.usr_pwd[j] != '\0';j++)
{
if((usr_msg.usr_pwd[j] >= 'A' && usr_msg.usr_pwd[j] <= 'Z') || (usr_msg.usr_pwd[j] >= 'a' && usr_msg.usr_pwd[j] <= 'z'))
letter++;
if(usr_msg.usr_pwd[j] >= '0' && usr_msg.usr_pwd[j] <= '9')
number++;
if(usr_msg.usr_pwd[j] == ' ')
break;
}
if(i > 20 || j < i || letter == 0 || number == 0)
{
printf("Re-enter an user-password : ");
}
else
{
printf("Password set successfully!\n");
break;
}
}
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • mzhan017 2022-03-10 17:56
    关注

    char *name_buf = NULL;这句后面要对这个buff申请内存,malloc

    评论

报告相同问题?

问题事件

  • 创建了问题 3月9日

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM