Thread: Getting error "Segmentation fault (core dumped)" during exectution

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Getting error "Segmentation fault (core dumped)" during exectution

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char prn_stars(int s);
    
    
    int main(void){
    int avg;
    scanf("%d", &avg);
    printf("%s",prn_stars(avg));
    
    
    }
    char prn_stars(int i){
            if (i >= 200)
                    printf("*");
            if (i > 200)
                    printf("*");
            if (i > 100)
                    printf("*");
    }
    prn_stars is a function to print stars

  2. #2
    spaghetticode
    Guest
    Your function doesn't return anything.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Your main function doesn't return any value, it should since it's declared with the return type int. This isn't causing the segfault however, the problem is with your printf statement and your prn_stars function. The %s in printf expects the first argument to be a null-terminated string. The argument is in fact nothing (afaik nothing = whatever happens to be in the eax-register in x86), since your prn_stars function does not return any value. Even if it did, the return type of char is wrong, since that is just a single char, and not a null-terminated string. So, either:
    - turn prn_stars into a function that returns void, and skip the printf statement in main
    - make prn_stars return a null-terminated string with the appropriate number of stars
    Last edited by iceaway; 10-31-2011 at 12:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  2. how to fix "Segmentation fault (core dumped)"
    By kapil1089thekin in forum C++ Programming
    Replies: 5
    Last Post: 08-20-2010, 04:24 AM
  3. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  4. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread