Thread: Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    Segmentation fault (core dumped)

    i cant figure out why this program i wrote this morning is giving me a seg fault
    im also new here
    Code:
    #include<stdio.h>  #include<string.h>
    #include<unistd.h>
    
    
    //simple program that takes data and then prints it :-)
    
    
    int main() {
    
    
    int i;
    int x;
    
    
    struct input {
    
    
    char input1[9];
    
    
    };
    
    
    struct input inputa;
    
    
    puts("this program accepts alot of input from your keyboard,9 chars");
    
    
    sleep(2);
    
    
    printf("enter your first string value\n");
    scanf("%s",inputa.input1);
    printf("enter your second value\n");
    scanf("%s",inputa.input1);
    printf("enter your third value\n");
    scanf("%s",inputa.input1);
    printf("enter your fourth string value\n");
    scanf("%s",inputa.input1);
    printf("enter your fifth value\n");
    scanf("%s",inputa.input1);
    printf("enter your 6th value\n");
    scanf("%s",inputa.input1);
    printf("enter your 7th string value\n");
    scanf("%s",inputa.input1);
    printf("enter your 8th value\n");
    scanf("%s",inputa.input1);
    printf("enter your 9th value\n");
    scanf("%s",inputa.input1);
    
    
    sleep(2);
    
    
    
    
    
    
    printf("now it will print out all the ........ you just typed above!\n");
    
    
    for(x=0;x<9;x++)
       printf("%s\n\n",inputa.input1[x]);
    
    
    
    
    printf(" DIDNT FEEL LIKE IT HAHAHAHAHAHAHAHA(JK)");
    
    
    return(0);
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet:
    Code:
    struct input {
    char input1[9];
    };
     
    struct input inputa;
    ...
    for(x=0;x<9;x++)
       printf("%s\n\n",inputa.input1[x]);
    You are telling printf to print a c-string but you are actually passing a single character.

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Only the last thing typed will be saved, as your program is overwriting the same 9 bytes every time it accepts input.

    You could fix this by having the struct be one of these three, and then fixing your input/output statements to match:

    Code:
    char input[9][xx]; /* xx is maximum string length */
    
    char *input[9]; /* malloc() each one however long you want, fixed limit of 9 strings */
    
    char **input; /* both number of strings and length of strings are dynamic */

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    per jimblumberg, the printf format %s wants an address, not a character. since you are passing a character, it will be used as an address and probably cause a segfault because it won't be valid.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    thanks

    i think im understanding now thank you

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    jimblumberg, then how do i make it print out the whole string instead of just a single character?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Something like:
    Code:
    printf("%s\n\n",inputa.input1);

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  2. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  3. Replies: 3
    Last Post: 06-05-2010, 02:58 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM