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

  • Home
  • SEARCH
  • 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 8876543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:14:17+00:00 2026-06-14T19:14:17+00:00

How can I pass values to struct variable I’m trying to get the employee

  • 0

How can I pass values to struct variable I’m trying to get the employee information from the user then write them in a file, but I got a segmentation fault after entering employee name.
This is my code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct record_em{
    int id;
    char name[20];
    int salary;
    int age;
};

int main( void )
{
    struct record_em employee;
    FILE *fp;
    int id, salary, age;
    char name[20];
    int n=1;

    fp = fopen("empRecord.dat","a");
    while(n==1){
        printf("\nEnter Employee ID\n");
        scanf("%d",&id);
        employee.id=id;
        printf("\nEnter Employee Name\n");
        scanf("%s",name);
        employee.name=name;
        printf("\nEnter Employee Salary\n");
        scanf("%d",&salary);
        employee.salary=salary;
        printf("\nEnter Employee Age\n");
        scanf("%d",&age);
        employee.age=age;
        fwrite(&employee,sizeof(employee),1,fp);
        printf("Enter 1 to add new record \n");
        scanf("%d",&n);
    }

    fclose(fp);

    return 0;
    }

Output (taken from comment):

Fatmahs-MacBook-Air:~ fatmah$ gcc -o em em.c
Fatmahs-MacBook-Air:~ fatmah$ ./em
Enter Employee ID
88
Enter Employee Name
uu
Segmentation fault: 11 
  • 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-14T19:14:18+00:00Added an answer on June 14, 2026 at 7:14 pm

    Change

    scanf("%s",name);
    employee.name=name;
    

    to

    scanf("%s",name);
    strcpy(employee.name, name);
    

    Of, better still, as suggested by Dukeling & hmjd

    scanf("%19s", employee.name);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I pass a variable value from a template using JavaScript without using
I can't seem to understand why I cant pass any values with the following
I'm generating a string of values (colors) like so: 'red|green|blue|yellow|orange|black' Can I pass this
How can I pass the result from a scalar [single row, single value] query
I want to pass a struct within a method so i can change it's
Can I pass value to Navigation Context like this: NavigationContext.QueryString[param1] = PARAM1; Is it
Can I pass a value out of a javascript function and back to the
How can I pass an object of a MyClass (C#) by Parameter-by-Value to a
How can I pass parameters in an OLE DB Source to call a table-valued
Hi can some one point me some guidance, i pretend to pass the value

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.