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

This is a discussion on Getting error "Segmentation fault (core dumped)" during exectution within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Your function doesn't return anything.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  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, 06:14 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21