Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    1

    Segmentation fault

    Hey!
    I keep getting a segmentation fault and I have no clue how to fix it.
    Does anyone know what I'm doing wrong?
    Code:
    #include "libpynq.h"
    #include <stdio.h>
    #include <string.h>
    #define MAXWORDS 10
    #define MAXLENGTH 30
    
    
    int numberOfWordsInDict(char dict[MAXWORDS][MAXLENGTH])
    {
        int numbers=0;
        for(int i=0; i<MAXWORDS; i++){
            for(int j=0; j<MAXLENGTH; j++){
                scanf("%c", letter);
                while(dict[j][i] != '\0'){
                    numbers++;
                }
            }
        }
        printf("The dictionary contains %d words.\n", numbers);
    }
    
    
    int main (void)
    {
        char array[MAXWORDS][MAXLENGTH] = {{""}};
        char input;
        printf("Command [qnaprslo]? ");
        scanf(" %c", &input);
    
    
        while(input != 'q')
        {
            if(input == 'n')
            {
                numberOfWordsInDict(array[MAXWORDS][MAXLENGTH]);
                printf("Command [qnaprslo]? ");
                scanf(" %c", &input);
            }
            printf("Unknown command '%c'\n", input);
            printf("Command [qnaprslo]? ");
            scanf(" %c", &input);
        }
        if(input == 'q')
        {
            printf("Bye!\n");
            return 0;
        }
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    First of all, turn on your warnings and turn up the warning level to the highest level.

    Code:
    scanf("%c", letter);
    Where is letter defined? Should it be "&letter"?

    return values from scanf() should be checked to see if it returned a value correctly.

    Please show "libpynq.h".

    Please clean up any errors and warnings shown by the compiler, then repost.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > numberOfWordsInDict(array[MAXWORDS][MAXLENGTH]);
    You don't specify the size of the array when you call the function.

    It's only
    numberOfWordsInDict(array);
    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.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    424
    Quote Originally Posted by rstanley View Post
    First of all, turn on your warnings and turn up the warning level to the highest level.

    Code:
    scanf("%c", letter);
    Where is letter defined? Should it be "&letter"?

    return values from scanf() should be checked to see if it returned a value correctly.

    Please show "libpynq.h".

    Please clean up any errors and warnings shown by the compiler, then repost.
    Pretty sure libpynq will be the BSP for the Digilent Pynq development board.

    If it is, it will be very long and only slightly useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault: 11?
    By calis1621 in forum C Programming
    Replies: 2
    Last Post: 10-06-2013, 09:36 PM
  3. Segmentation Fault
    By jacobj86 in forum C Programming
    Replies: 3
    Last Post: 03-15-2012, 08:00 PM
  4. Segmentation fault
    By Calavera in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:19 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread