Segmentation Fault

This is a discussion on Segmentation Fault within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm getting a segmentation fault and I'm trying to sort it out but I have no idea why ...

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Segmentation Fault

    Hey guys, I'm getting a segmentation fault and I'm trying to sort it out but I have no idea why I'm getting it.

    Code:
     char input[20]; 
      printf("Qual o .txt que deseja abrir ?\n");
      fgets(input,20,stdin);
      FILE *fp;
      input[strlen(input)-1] = '\0';
      fp = fopen(input,"r"); 
      if (fp==NULL)
      {printf("Nao foi possivel abrir o seu ficheiro\n");}
      else leadj(fp,18);
    In case I input a .txt that doesn't exist the printf works fine but when I do input a .txt that does exist the leadj(fp,18) is getting me a segmentation fault and I have no clue why taking in fact that if I do it on main it works

    Any ideas ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,644
    Perhaps the error lies in leadj.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Found out what my error is.
    leadj receives a document name and I was passing it the document's pointer.

    Thanks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > leadj receives a document name and I was passing it the document's pointer.
    You were passing a FILE* to something expecting a char* ?

    Was your compiler complaining about it?

    Or were you just ignoring the warnings?
    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
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    It was giving me no warnings, just a segmentation fault.
    But it's all fixed now!
    Thanks

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Don't tell me you were trying to call the function without a prototype in scope.
    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.

  7. #7
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    I was just passing the function the document pointer instead of the document itself.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Yes I know, but my question is how it failed to be spotted by the compiler BEFORE you tried to run it (and have it crash horribly).

    This was an easily avoided problem in a well-written program with a decent compiler.

    You see, if this isn't solved, you're just going to run into the same problem over and over.
    And we'll get bored of telling you.
    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.

  9. #9
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by Salem View Post
    Yes I know, but my question is how it failed to be spotted by the compiler BEFORE you tried to run it (and have it crash horribly).

    This was an easily avoided problem in a well-written program with a decent compiler.

    You see, if this isn't solved, you're just going to run into the same problem over and over.
    And we'll get bored of telling you.
    I have no idea.
    Instead of writing the document pointer I wrote the document name and it worked.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > Instead of writing the document pointer I wrote the document name and it worked.
    Great - now figure out how to make the compiler complain about the old version.

    If you don't, I guarantee you'll be spending most of your time chasing stupid bugs.
    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.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    Are you still compiling at the command line? If so please show how you are now compiling your programs (show the complete command line for your compile).

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By gums in forum C Programming
    Replies: 11
    Last Post: 05-03-2007, 07:06 AM
  2. Segmentation Fault :(
    By DarkDot in forum C++ Programming
    Replies: 39
    Last Post: 04-07-2007, 04:16 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  4. segmentation fault when using gcc
    By stodd04 in forum C Programming
    Replies: 6
    Last Post: 02-14-2005, 06:34 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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