Thread: Segmentation fault?

  1. #1
    Registered User
    Join Date
    May 2010

    Segmentation fault?

    I can' t stand any more...i need help...
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    struct Employee{
           int i;
           int age;
           char name[30];
    void init(struct Employee *buf, int count){
         int i;
             for(i=0; i<count; i++){
                      printf("Please type a age between 20 and 65:\n");
                      scanf("%d", &(buf->age));
    int main(int argc, char *argv[]){
           int i, n;
           struct Employee *buf;
           n = atoi(argv[2]); // n einai to megethos tou pinaka
           buf = (struct Employee *)malloc(n*sizeof(struct Employee));
           /*  Edw tha kalesoume tin synartisii */
           init(buf, n);
           return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    scanf("%d", &(buf->age));
    This should be more something like:
    scanf( "%d", &( buf[ i ].age ) );
    You should also check your return values from malloc and your other functions to see that they're doing what you expect of them.

    Hope is the first step on the road to disappointment.

  3. #3
    Make Fortran great again Epy's Avatar
    Join Date
    Sep 2009
    California, USA
    nevermind, quzah got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM