Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:48:50+00:00 2026-06-14T21:48:50+00:00

I’ve got a phonebook app that I’ve been trying to add malloc to over

  • 0

I’ve got a phonebook app that I’ve been trying to add malloc to over the last few days, but since I’m new to C and the book I have doesn’t go into the detail I would like, I’m not sure of all the tricks. Is it possible to use malloc when the user inputs information, and then use free() as a way to delete user input when the user elects to delete an individual from the phonebook? Right now regardless of whether I use free() or not, after the user deletes an entry and then tries to check the phonebook, the program crashes. I’m assuming(but we know what that means) that it has something to do with improperly freeing or not freeing the memory. Here is the code.

#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;
}        

I didn’t add the free() code because regardless it seemed to be getting the same results. Why would the program crash only after I have deleted a name from the list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:48:51+00:00Added an answer on June 14, 2026 at 9:48 pm

    Step1: Isolating the issue:

    //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");
           }
         }
    }
    

    Step2: Possible Problem

    (*counter)++ //why do this?
    

    This is executed, when the name to be deleted is found. Why would you increment the count of all names in the list? I think this should be a decrement instead, or shouldn’t be done within the loop at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.