Malloc with structs - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Malloc with structs

Over the last couple months I have been trying to pick up and learn the C language, so forgive me for my noobiness( thats a word right? haha). I created a phonebook app, but when I created it, I didn't use any dynamic memory allocation, so now I'm trying to go back and add that to the program. The way I set up my program was that each part of the structure had 2 functions that were associated with it( ex. First_Name has a function that received the user input, and then a function that prints the user input into the phonebook), and what I'm trying to accomplish is make each string within the structure into a char pointer and then in each function that receives input will have malloc in order to manage the memory.

So far I've tried to implement this and the program will allow me to choose to add a contact, and when I enter the name and return back to the menu again, choosing to add another name or choosing to show the phonebook results in the program closing. I'm at a loss as to how to fix this. below is a watered down version of the original code, just enough to allow for first names to be entered. I figured once I can get it working for one string within the structure, I can get it to work for the whole program.
[code]
#include
#include
#include
#include

#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("
Phone Book Application
");
printf("1) Add friend
2) Delete friend
3) Show a friend
4) Show phonebook
5)Exit
");
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) {
friends=(fr*) malloc(BUFFER*sizeof(fr));
printf("Enter a first name
");
scanf("%s",friends[*counter].First_Name);



}
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);
}
}
[/code]
Sign In or Register to comment.