Thread: Segmentation fault?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    Segmentation fault?

    I can' t stand any more...i need help...
    Code:
    #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);
    
           system("pause");
           return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    scanf("%d", &(buf->age));
    This should be more something like:
    Code:
    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.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    1,171
    nevermind, quzah got it.
    Code:
    PROGRAM main
    IMPLICIT NONE
    PRINT *, "I love Fortran!"
    END PROGRAM main

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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM