So I'm trying add malloc to a phonebook application that I created, but since I'm kind of new to C I'm not sure if what I'm doing is correct. I've run into a small problem, but I've read through the beginner book that I have, and it doesn't go though as much detail as I would like, I can't tell by searching Google if I'm just completely wrong in how I set up the malloc or if there is something else I missed.
Basically what I've got are 4 arrays in my structure, First_Name, Last_name,home,cell. Each one of these have 2 functions, a function that gets the info from the user and a function that prints and adds the user info to the phonebook. What I've got right now is a small snipit of the original code that only adds the first name to the phonebook(so it's not the entire code) and in each function that gets the user input, I want to add the malloc function. Right now I've only got the first name and the first malloc set up, but the issue I have is that when I go to check the phonebook to see if the name was entered successfully, the program quits. If I take out the malloc, it works successfully.
#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;
void menu(fr * friends, int *counter, int user_entry, int i);
void setFirst(fr *, int *, int i);
char getFirst(fr *, int i);
void add_contact(fr * friends, int *counter, int i);
void print_contact(fr * friends, int *counter, int i);
int main()
{
int user_entry = 0;
fr *friends;
int counter = 0;
int i = 0;
menu(friends, &counter, user_entry, i);
getch();
return 0;
}
//Menu function
void menu(fr * friends, int *counter, int user_entry, int i)
{
do {
int result;
printf("\nPhone Book Application\n");
printf
("1) Add friend\n2) Delete friend\n3) Show a friend\n4)Showphonebook\n5)Exit\n");
scanf("%d", &user_entry);
if (user_entry == 1) {
add_contact(friends, counter, i);
}
if (user_entry == 2) {
}
if (user_entry == 3) {
}
if (user_entry == 4) {
print_contact(friends, counter, i);
}
} while (user_entry != 5);
}
void setFirst(fr * friends, int *counter, int i)
{
// THE MALLOC FUNCTION!
friends = (fr *) malloc(BUFFER * sizeof(fr));
printf("Enter a first name \n");
scanf("%s", friends[*counter].First_Name);
if (friends != NULL) {
free(friends);
}
}
char getFirst(fr * friends, int pos)
{
printf("%s ", friends[pos].First_Name);
return *friends[pos].First_Name;
}
void add_contact(fr * friends, int *counter, int i)
{
setFirst(friends, counter, i);
(*counter)++;
}
void print_contact(fr * friends, int *counter, int i)
{
for (i = 0; i < *counter; i++)
if (strlen(friends[i].First_Name)) {
getFirst(friends, i);
}
}
Looking to give a big green check mark to whoever can help me out here.
setFirst, you'refreeing yourfriendsbuffer, in essence saying I don't need this anymore. When you do this, that memory just goes away. If you're going to dynamically allocate structures for the caller, you either have to provide a separate deallocation function, or let your user know it's their responsibility to clean up that structure. Also, you're only ever changing the local copy of the friends pointer. If you want to point the caller's pointer to a new buffer, you need to change the argument type tofr**.scanf("%s",invitingBufferOverflow);is just as terrible (or worse) asgets(invitingBufferOverflow)