When the user enters information for a friend, I want the pointer to allocated appropriate space and the friend information be stored in this allocated space. i’ve read snippits other places that mention using a buffer array as an argument to scanf, but I’m just having a putting this all together. Here is what I have so far.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
//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[5];
int buffer[50];
int counter=0;
int i=0;
for(i=0;i<5;i++)
{
friends[i].First_Name = (char*) malloc(sizeof(char) * 64);
free(friends[i].First_Name);
}
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)"
"Showonebook\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)
{
//malloc issue **
friends=(fr*) malloc(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);
}
}
This is only part of the code obviously, and as of right now I get a segmentation fault after I enter a name into the add name function. It loops to the menu one last time before quitting. I realize that I have gone wrong somewhere, and I would like to try and fix this with the buffer solution. Solutions anyone?
Your
frvariable is a pointer to some location on the stack that’s already got space allocated. You shouldn’t be using that – it’s already initialized and you can’t malloc it (or at least you never ever should). You should instead pass a pointer and initialize it.That is to say:
And then when you want to allocate space
Furthermore, you need to initialize the elements of your structure if you want to use them, since they’re all pointers. This would be done similarly
Or it would be
friends[i]->First_Namedepending on how you’ve sent it along to your function.A good solution for you would just be to initialize the members of the struct inide your
mainHere I’ve chosen 64 to be the length of string. You can set it to whatever you think works.
The bottom line is that you have to allocate space for a pointer before you can use it. Otherwise it’s not pointing to any memory you can use and you’ll get a segmentation fault. Also, don’t forget to free once you’re done with the memory you’ve allocated. Do remember that you can’t use it once you’ve freed it (unless you malloc it again).