Thread: What am I doing worng? Segmentation fault, core dump

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Unhappy What am I doing worng? Segmentation fault, core dump

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main( )
    {
    int x;
    int y;
    int choice;
    
    printf ( "Hello! This is the second version of my calculator\n" );
    printf ( "Select addition or subtraction\n" );
    printf ( "1.Addition\n2.Subtraction\n");
    scanf ("%d", choice);
    
    if ( choice = 1 )
        {
        printf ( "You chose addition\n" );
        printf ( "Input first integer\n" );
        scanf ("%d", x);
        printf ( "Input the second one\n" );
        scanf ("%d", y);
        printf ("%d", x + y );
        }
    
    
    }
    I do not have a code for choice 2, but why doesn't choice 1 work?
    What am I doing wrong????
    It just gives me a core dump, segmentation faul error after I choose 1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf ("&#37;d", x);
    You forgot the &
    as in
    scanf ("%d", &x);

    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:14: warning: format argument is not a pointer (arg 2)
    foo.c:16: warning: suggest parentheses around assignment used as truth value
    foo.c:20: warning: format argument is not a pointer (arg 2)
    foo.c:22: warning: format argument is not a pointer (arg 2)
    foo.c:27: warning: control reaches end of non-void function
    foo.c:7: warning: 'x' might be used uninitialized in this function
    foo.c:8: warning: 'y' might be used uninitialized in this function
    foo.c:9: warning: 'choice' might be used uninitialized in this function
    If your compiler is gcc based, then consider increasing the warning level to spot all those scanf issues.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    After you fix the error Salem noted:
    Code:
    if ( choice = 1 )
    You want two equal signs: choice == 1. == tests for equality, = assigns a value. Your if() statement stores 1 to choice, and then boils down to if(1), something that will always prove true. Again, turning up gcc's warnings (if you're using gcc) will probably catch something like this.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Again, turning up gcc's warnings (if you're using gcc) will probably catch something like this.
    You mean the
    foo.c:16: warning: suggest parentheses around assignment used as truth value
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Lightbulb Thanks all!

    I get it now! Without that & operator, u do not tell the program to look for it!



    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  3. Core Dump / Segmentation Fault
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-08-2003, 08:24 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 core dump - need help
    By knight101 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 04:43 PM