Thread: SegFault Help!

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    SegFault Help!

    I give the input and when i press enter i get a segfault! can you help me?
    thanks

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define N 100
    #define M 37
    #define AEM_MAX 4
    
    int main (int argc, char *argv[]) {
        
        char studentdata[N][M], c;
        int i, pos;
        
        
    
        for (i=0;i<N;i++) {
            fgets(studentdata[i],M,stdin);
            if (studentdata[i][0]=='-') {
                break;
            }
            else {
                do {
                    c=getchar();  /*Aporofhsh tou kenou*/
                } while (c!='\n');
            }
            }
        
        for (i=0;i<N;i++) {
            studentdata[i][31]='\0';  /*antikatastash tou ' ' me to \0 */
        }
        
        for (i=0;i<N-5;i++) { /*Bazw -5 giati den thelw na exw thn telia ston elegxo*/
            if (ispunct(studentdata[i])) {
                pos=i;
            }
        }
        printf("%s. ",studentdata[pos+1]); /*Ektypwsh tou prwtou grammatos tou onomatos*/
        for (i=0;i<pos;i++) {
            if (isalpha(studentdata[i])) {
                printf("%s",studentdata[i]);
            }
        }
        printf(" ");
        for (i=0;i<AEM_MAX;i++) {                 /*Dhlwsa to AEM_MAX ws 4 dioti to AEM einai to polu 5 arithmoi*/
            if (isdigit(studentdata)) {            /*opote mou arkei o elegxos apo to stoixeio 0 ews to stoixeio 4*/
                printf("%s",studentdata[i]);
            }
        }
        printf(" ");
        
            
       
        
                     
    
    
    
    
    
    
        return(0);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    My first suggestion would be that you use meaningful variable names. For example instead of N use something like NUMBER_OF_STUDENTS and instead of M use something like NAME_LENGTH. It looks like you may have switched M and N is several of your loops, so maybe using meaningful names may help insure you're using the correct sizes for each loop.

    You also are using several magic numbers (5, 31) you may want to create meaningful named constants for these values as well.

    Also without knowing what you're inputting into the program it is hard to go much farther.

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Code:
    $ gcc -std=c99 -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:31:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    foo.c:37:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    foo.c:43:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    foo.c:7:15: warning: unused parameter ‘argc’ [-Wunused-parameter]
    foo.c:7:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
    Your use of 'is' functions is wrong.

    Which presumably then leaves pos uninitialised and then garbage in your later loops.
    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
    Dec 2015
    Posts
    2
    Thanks for the comments, i will try to improve my programm(second month learning C programming).
    So its better to use strchr to check for dots and etc?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    There are many ways of solving the same problem.
    "Better" is rather subjective.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Segfault help
    By edishuman in forum C Programming
    Replies: 4
    Last Post: 11-03-2011, 05:08 PM
  2. Segfault
    By astral in forum C Programming
    Replies: 8
    Last Post: 06-18-2011, 11:20 PM
  3. another SEGFAULT
    By cerr in forum C Programming
    Replies: 8
    Last Post: 01-14-2010, 11:04 AM
  4. Sometimes segfault, sometimes not
    By jcafaro10 in forum C Programming
    Replies: 18
    Last Post: 04-07-2009, 06:53 PM
  5. segFault
    By Fox101 in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 12:00 AM