我有一个电话本应用程序,我一直试图在过去几天添加malloc,但是因为我是C的新手而且我的书没有详细说明我想要的,我不确定所有的技巧。当用户输入信息时是否可以使用malloc,然后当用户选择从电话簿中删除个人时使用free()
作为删除用户输入的方法?现在无论我是否使用free()
,用户删除条目然后尝试检查电话簿,程序崩溃。我假设(但我们知道这意味着什么)它与不正当地释放或不释放内存有关。这是代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define BUFFER 50
//Structure for contacts
typedef struct friends_contact{
char *First_Name;
char *Last_Name;
char *home;
char *cell;
}fr;
//Function declarations
void menu(fr*friends ,int* counter,int user_entry,int i,char newbuddy[]);
void setFirst(fr*,int *,int i,char newbuddy[]);
char getFirst(fr*,int i);
void setLast(fr*friends, int* counter, int i,char newbuddy[]);
char getLast(fr*friends , int i);
void setHome(fr*friends, int* counter, int i,char newbuddy[]);
char getHome(fr*friends, int i);
void setCell(fr*friends, int* counter, int i,char newbuddy[]);
char getCell(fr*friends, int i);
void add_contact(fr*friends,int* counter,int i,char newbuddy[]);
void print_contact(fr*friends ,int* counter, int i);
char delete_contact(fr*friends ,int* counter, int i);
char show_contact(fr*friends ,int* counter, int i);
int main() {
int user_entry=0;
fr friends[5];
char newbuddy[BUFFER];
int counter=0;
int i=0;
menu(friends, &counter,user_entry,i,newbuddy);
getch();
return 0;
}
//Menu function
void menu(fr*friends,int* counter,int user_entry, int i,char newbuddy[]) {
do{
int result;
printf("\nPhone Book Application\n");
printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit\n");
scanf("%d", &user_entry);
if(user_entry==1)
{
add_contact(friends,counter,i,newbuddy);
}
if(user_entry==2)
{
delete_contact(friends ,counter,i);
}
if(user_entry==3)
{
result=show_contact(friends ,counter,i);
if(result==0){
printf("\nName not Found\n");
}else{
result;
}
}
if(user_entry==4)
{
print_contact(friends, counter,i);
}
}while(user_entry!=5);
}
//Start of Set functions. Each entry has its own set function that gathers the data
void setFirst(fr*friends, int* counter, int i,char newbuddy[]) {
printf("Enter a first name \n");
scanf("%s",newbuddy);
friends[*counter].First_Name=malloc(BUFFER*strlen(newbuddy));
strcpy(friends[*counter].First_Name, newbuddy);
}
void setLast(fr*friends, int* counter, int i,char newbuddy[]) {
printf("Enter a last name \n");
scanf("%s",newbuddy);
friends[*counter].Last_Name=malloc(BUFFER*strlen(newbuddy));
strcpy(friends[*counter].Last_Name, newbuddy);
}
void setHome(fr*friends, int* counter, int i,char newbuddy[]) {
printf("Enter a home number \n");
scanf("%s",newbuddy);
friends[*counter].home=malloc(BUFFER*strlen(newbuddy));
strcpy(friends[*counter].home, newbuddy);
}
void setCell(fr*friends, int* counter, int i,char newbuddy[]) {
printf("Enter a cell number \n");
scanf("%s",newbuddy);
friends[*counter].cell=malloc(BUFFER*strlen(newbuddy));
strcpy(friends[*counter].cell, newbuddy);
}
//Start of Get functions. Each function sends the data to the executing function.
char getFirst(fr*friends , int pos) {
printf("%s ", friends[pos].First_Name);
return *friends[pos].First_Name;
}
char getLast(fr*friends , int pos) {
printf("%s\n", friends[pos].Last_Name);
return *friends[pos].Last_Name;
}
char getHome(fr*friends , int pos) {
printf("(Home) ""%s\n", friends[pos].home);
return *friends[pos].home;
}
char getCell(fr*friends , int pos) {
printf("(Cell) ""%s\n", friends[pos].cell);
return *friends[pos].cell;
}
//This function allows for the all the set functions to be added.
void add_contact(fr*friends,int* counter,int i,char newbuddy[]) {
setFirst(friends,counter,i,newbuddy);
setLast(friends,counter,i,newbuddy);
setHome(friends,counter,i,newbuddy);
setCell(friends,counter,i,newbuddy);
(*counter)++;
}
//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter, int i)
{
char name_search[50]={'\0'};
char Delete[5]={'\0'};
printf("Search by last name\n");
scanf("%s",name_search);//Name entry
for(i=0;i<*counter;i++)
{
if(strcmp(name_search,friends[i].Last_Name)==0)//Copys over the name entered
{
strcpy(friends[i].Last_Name,Delete);
(*counter)++;
printf("\nName has been deleted\n");
}
}
}
//This function prints out all the contact information
void print_contact(fr*friends ,int* counter, int i) {
for( i = 0; i < *counter; i++)
if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&& strlen(friends[i].home)&& strlen(friends[i].cell ))
{
getFirst(friends, i);
getLast(friends, i);
getHome(friends, i);
getCell(friends, i);
}
}
//Displays the contact in which you are searching for.
char show_contact(fr*friends ,int* counter, int i)
{
char name_search2[50]={'\0'};
printf("Please enter a last name\n");
scanf("%s",name_search2);
for(i=0;i<*counter;i++)
{
//If the name is found, it reaturns the contact info.
if(strcmp(name_search2,friends[i].Last_Name)==0)
{
return printf("%s " "%s" "(Home)""%s""(Cell)" "%s\n",friends[i].First_Name, friends[i].Last_Name,friends[i].home,friends[i].cell);
}
}
return 0;
}
我没有添加free()
代码,因为无论它似乎得到相同的结果。为什么程序在我从列表中删除名称后才会崩溃?
答案 0 :(得分:0)
//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter, int i)
{
char name_search[50]={'\0'};
char Delete[5]={'\0'};
printf("Search by last name\n");
scanf("%s",name_search);//Name entry
for(i=0;i<*counter;i++)
{
if(strcmp(name_search,friends[i].Last_Name)==0)//Copys over the name entered
{
strcpy(friends[i].Last_Name,Delete);
(*counter)++;
printf("\nName has been deleted\n");
}
}
}
(*counter)++ //why do this?
当找到要删除的名称时执行此操作。为什么要增加列表中所有名称的数量?我认为这应该是一个减量,或者根本不应该在循环内完成。