segmentation fault when using gcc

This is a discussion on segmentation fault when using gcc within the C Programming forums, part of the General Programming Boards category; when I try to run this program it gives me an error message saying "segmentation fault". Any insight would be ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16

    Question segmentation fault

    when I try to run this program it gives me an error message saying "segmentation fault". Any insight would be greatly appreciated. It compiles with gcc just fine. But I did not get this error when I compiled and ran it with Dev-C++.

    Code:
    ///////////////////////////////////////////////////////////////////////////////
    //
    // Name: Sean Todd
    // Date: Feb 14, 2005
    // Desc: this program asks a usr to enter an int and tells the usr if it 
    //is between 5 and 10 and also checks to see if valid data was entered
    //
    //////////////////////////////////////////////////////////////////////////////
    
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
    
       char input;
    
       printf("\nName: Sean Todd");
       printf("\nPlease enter an integer: ");
       scanf("%c", input);
    
       if(!isalnum(input))
       {
          printf("\nNo Data Could Be Read");
       }
       else if(!isdigit(input))
       {
          printf("\nData Was NOT an Integer");
       }
       else if(input > 53 && input <=57)
       {
          printf("\nThe Number, %c is between 5 and 10", input);
       }
       else
       {
          printf("\nThe Number, %c, is NOT between 5 and 10", input);
       }
    
       printf("\n\n");
    
       return 0;
    }//end of main
    thanks in advance
    -Sean
    Last edited by stodd04; 02-14-2005 at 09:35 AM.

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    According to a reference on scanf(), these arguments must be pointers: if you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&)
    Code:
    scanf("%c", &input);
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    ahh... stupid me... forgot the "&" when I rewrote it on the server. Thank you very much for catching my stupid mistake

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > It compiles with gcc just fine.
    gcc -W -Wall -ansi -pedantic -O2
    Now that catches an awful lot of things which may be wrong with your code, including mistakes in printf and scanf calls.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    i thought that if -Wall is on you dont need -W on too

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    According to the gcc man pages there are many Warning Options, e.g., -w, -W, -Wall, -Werror, -pedantic, -pedantic-errors, etc...

    I do know for a fact -pedantic-errors caught different errors than -pedantic did. That may mean each flag serves its own purpose.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a list, it's been referenced here before, that lists what flags include the other flags. I'm sure if one were to search they could find it.

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

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

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