Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Malloc with structs Posted by jcmoney1010 on 9 Nov 2012 at 2:11 PM
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.
#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) Show phonebook\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) {
friends=(fr*) malloc(BUFFER*sizeof(fr));
    printf("Enter a first name \n");
    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);
     }
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.