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 8807849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:28:10+00:00 2026-06-14T02:28:10+00:00

When the user enters information for a friend, I want the pointer to allocated

  • 0

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?

  • 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-14T02:28:12+00:00Added an answer on June 14, 2026 at 2:28 am

    Your fr variable 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:

    fr *friends;
    

    And then when you want to allocate space

    friends = (fr*) malloc(sizeof(fr)* NUMBER OF FR YOU WANT);
    

    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

    friends[i].First_Name = (char*) malloc(sizeof(char)* (LENGTH OF STRING + 1));
    

    Or it would be friends[i]->First_Name depending 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 main

    int i;
    fr friends[5];
    for(i=0;i<5;i++)
    {
       friends[i].First_Name = (char*) malloc(sizeof(char) * 64);
       friends[i].Last_Name = (char*) malloc(sizeof(char) * 64);
       friends[i].home = (char*) malloc(sizeof(char) * 64);
       friends[i].cell = (char*) malloc(sizeof(char) * 64);
    }
    //
    //Do functions (without malloc'ing)
    //
    for(i=0;i<5;i++)
    {
       free(friends[i].First_Name);
       free(friends[i].Last_Name);
       free(friends[i].home);
       free(friends[i].cell);
    }
    

    Here 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).

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

Sidebar

Related Questions

Possible Duplicate: Going from database to sessions A user enters information, which I stored
I want to display information of user stored in a MS Access database. The
In this scenario: User enters some information into a textbox User clicks a button
I want to display results instantly while user enters some information (like google search).
If a user enters a URL with a action=editDocument, I want to be able
When the user enters info I want all the controls to look and act
I have a webpage that has a textbox. When the user enters information into
Here's what I have. User enters values into text boxes (personal information etc.) and
How can I make this get a specific users data, Example user enters his
I have a form field where a user enters contact information, including name, email,

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.