核心转储?
Getting core dumped?
#include <stdio.h>
#include <cs50.h>
#include <stdbool.h>
typedef struct shop {
string custName;
string custAdd;
string custNum;
string custClothPrice;
struct shop* next;
}
shop;
int main (void)
{
struct shop* head;
head = NULL;
struct shop* pointer=NULL;
printf("**********WELCOME TO AZIZIYAH CREATIONS**********\n\n");
char cont = 'y';
printf("Please enter customer details\n");
printf("Please Enter NAME : ");
head->custName = GetString();
printf("Please Enter Address: ");
head->custAdd = GetString();
printf("Please Enter Cell No: ");
head->custNum = GetString();
printf("Please Enter Price : ");
head->custClothPrice = GetString();
return 0;
printf("Would you liked to add more customer(s)?: Y/N:");
cont = GetChar();
head->next = pointer;
if (cont == 'Y' || cont == 'y')
{
pointer = malloc(sizeof(shop));
pointer->next = NULL;
}
while (cont == 'y' || cont == 'Y' )
{
printf("Please enter customer details\n");
printf("Please Enter NAME : ");
pointer->custName = GetString();
printf("Please Enter Address: ");
pointer->custAdd = GetString();
printf("Please Enter Cell No: ");
pointer->custNum = GetString();
printf("Please Enter Price : ");
pointer->custClothPrice = GetString();
printf("Would you liked to add more customer(s)?: Y/N:");
cont = GetChar();
if (cont == 'Y' || cont == 'y')
{
pointer->next = malloc(sizeof(shop));
pointer = pointer ->next;
}
}
printf("\n\nDETAILS OF CUSTOMER ARE AS FOLLOWS:-\n");
struct shop* traverse;
traverse = head;
if (traverse->next == NULL)
printf("WAS FOUND NULL\n");
while (traverse->next != NULL)
{
printf("%s\n", traverse->custName);
printf("%s\n", traverse->custAdd);
printf("%s\n", traverse->custNum);
printf("%s\n", traverse->custClothPrice);
traverse = traverse->next;
}
printf("**********THANK YOU FOR VISITING**********\n");
}
使应用程序在首次输入后产生分段错误(核心转储)错误
输出
**********欢迎来到 AZIZIYAH CREATIONS **********
请输入客户详细信息
请输入姓名:asd
分段错误(核心已转储)
请帮忙
分配给 head->custName
会导致崩溃:您需要为 head
分配内存,然后才能将内容写入它指向的位置。如果你不这样做,你的程序就会崩溃。您可以像这样使用 malloc()
为 head
分配内存:
head = malloc(sizeof *head);
if (head == NULL) {
/* error handling code here */
}
记得插入适当的错误处理代码。
#include <stdio.h>
#include <cs50.h>
#include <stdbool.h>
typedef struct shop {
string custName;
string custAdd;
string custNum;
string custClothPrice;
struct shop* next;
}
shop;
int main (void)
{
struct shop* head;
head = NULL;
struct shop* pointer=NULL;
printf("**********WELCOME TO AZIZIYAH CREATIONS**********\n\n");
char cont = 'y';
printf("Please enter customer details\n");
printf("Please Enter NAME : ");
head->custName = GetString();
printf("Please Enter Address: ");
head->custAdd = GetString();
printf("Please Enter Cell No: ");
head->custNum = GetString();
printf("Please Enter Price : ");
head->custClothPrice = GetString();
return 0;
printf("Would you liked to add more customer(s)?: Y/N:");
cont = GetChar();
head->next = pointer;
if (cont == 'Y' || cont == 'y')
{
pointer = malloc(sizeof(shop));
pointer->next = NULL;
}
while (cont == 'y' || cont == 'Y' )
{
printf("Please enter customer details\n");
printf("Please Enter NAME : ");
pointer->custName = GetString();
printf("Please Enter Address: ");
pointer->custAdd = GetString();
printf("Please Enter Cell No: ");
pointer->custNum = GetString();
printf("Please Enter Price : ");
pointer->custClothPrice = GetString();
printf("Would you liked to add more customer(s)?: Y/N:");
cont = GetChar();
if (cont == 'Y' || cont == 'y')
{
pointer->next = malloc(sizeof(shop));
pointer = pointer ->next;
}
}
printf("\n\nDETAILS OF CUSTOMER ARE AS FOLLOWS:-\n");
struct shop* traverse;
traverse = head;
if (traverse->next == NULL)
printf("WAS FOUND NULL\n");
while (traverse->next != NULL)
{
printf("%s\n", traverse->custName);
printf("%s\n", traverse->custAdd);
printf("%s\n", traverse->custNum);
printf("%s\n", traverse->custClothPrice);
traverse = traverse->next;
}
printf("**********THANK YOU FOR VISITING**********\n");
}
使应用程序在首次输入后产生分段错误(核心转储)错误 输出 **********欢迎来到 AZIZIYAH CREATIONS **********
请输入客户详细信息 请输入姓名:asd 分段错误(核心已转储)
请帮忙
分配给 head->custName
会导致崩溃:您需要为 head
分配内存,然后才能将内容写入它指向的位置。如果你不这样做,你的程序就会崩溃。您可以像这样使用 malloc()
为 head
分配内存:
head = malloc(sizeof *head);
if (head == NULL) {
/* error handling code here */
}
记得插入适当的错误处理代码。