Segmentation Fault Error

This is a discussion on Segmentation Fault Error within the C Programming forums, part of the General Programming Boards category; Hi there, I'm a newbie in C.. I have this Segmentation Fault error from the compiler without any explanation where ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Mississauga
    Posts
    10

    Smile Segmentation Fault Error

    Hi there, I'm a newbie in C.. I have this Segmentation Fault error from the compiler without any explanation where is the error and where to start checking.. There are parts of my code..

    Code:
    #define SENTENCE 3
    
    struct dictionary_input {
        char word[80];
        char definition[SENTENCE_MAX][80];
        } dictionary[ENTRY_MAX];
    
    dictionary_input retrieved_definition() {
        printf("\nEnter keyword: ");
        gets(dictionary->word);
        printf("\nEnter definition up to 3 lines: ");
        for (i=0; i < SENTENCE; i++) {
            for(j=0; j < 80; j++) {
                printf("\nLine 1: ");
                gets(dictionary->definition[i][j]);
            }
        }
    }
    When I call this function, user will input a keyword and the definition up to 3 lines in keyword array and sentence array, respectively. Also, I have to use gets() in this function... While, scanf() is a lot better, lol... So, is there anyway to fix this? Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First thing would be to NEVER use gets(). Use fgets() instead and read the FAQ before posting.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    What is the definition of ENTRY_MAX? Zero?

    Or your definition dimensions need to be swapped around. I think arrays of arrays are column major, which means that the array actually stands to be 80 rows of 3 columns, as it is right now.
    Last edited by whiteflags; 01-25-2011 at 12:18 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
                gets(dictionary->definition[i][j]);
    to
           for (i=0; i < SENTENCE; i++) {
              fgets(dictionary->definition[i], 80, stdin);
             // strip new line if present
            }

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I assume inside the function you need to declare a local pointer
    struct dictionary_input *dictionary = dictionary[n] (an element of the global one), or it's passed as an argument.

    so that you can use it... dictionary->

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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