Thread: why core dump in this function?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    why core dump in this function?

    I have been searching through various sources, searched through the posts here, and I cannot figure it out why this function is segfaulting on me.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    
    #define SIZE 41
    
    
    void read_message(char[]);
    
    int main()
    {
       char message[SIZE];    
    
       read_message(message);
    
      return 0;
    }
    
    
    void read_message(char array[])
    {
        
        int ch;
        int i = 0; 
        
        printf("Enter your message:\n"); 
        
        while ( (ch=getchar()) != '\n' ) 
        {
            array[i] = ch;
            i++;
        } 
        
        array[i] = '\0';        
    }
    I mean when I execute, it asks for the message, and right after I enter it, the program crashes. I'm pretty sure it does not go past "while". It's so frustrating to be stuck on something so simple as reading an array of chars...
    Thanks in advance.

  2. #2
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    i had a similar question a week or so back. what i ended up doing, and i dont know if it helps u any. but i declared a global variable and then passed it back.

    ie
    Code:
    #include <stdio.h> 
    
    char testmain[5];
    
    int postchar( void )
    {
        testmain[0]='a';
        
           printf("%s",testmain);
           return;
    }
    
    int main( void )
    {
        postchar();
      printf("%s",testmain);
      
      getch();
      
      return 0;
    }
    hope that helps
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Allright, I went though it again, cleaned it all up, and it looks for now as this part is working! Thanks and sorry that I bothered you.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The first thing I notice is that you're trying to pass an array as a parameter. In C, you can not do this. Instead, pass a pointer to the first element of the array. You'll have to change some of your arithmetic to work with a pointer instead of directly interfacing with an array. Since this shouldn't have even been legal, what compiler are you using?

    while ( (ch=getchar()) != '\n' )
    That's some ugly code! (no offense) Try splitting that up into two statements and joining them in the loop with a logical AND.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't see anything syntactically (sp?) wrong with the program. In fact, it runs fine on my computer. Are you ending your input with something like EOF instead of ENTER ('\n')? Is your message more than 40 characters long?

    I do see that read_message() is seriously lacking in security. It looks like a direct implementation of gets() which we all know is an evil function.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    The problem was somewhere else...

    Is it really wrong? It looks almost like the function to read a line from my textbook, which is KN King.
    What I noticed after a couple of more hours of working on this, is that my problem with core dump lies in the loop of the next function. I inserted a control printf statement inside my loop, and ran the program (it compiles using gcc, but not DevC++). It displayed my printf about a hundered something of times and then core was dumped. Can you take a look? (I'm not supposed to use pointers here... only array subscripting)
    Code:
    int clean_array(char array[], char clean[], int size)
    {
        int i, j = 0;
    
        for(i = 0; i < size; i++)
        {
            while(array[i] != '\0')          /* read original array */
            {   
                if( isalpha(array[i]) )
                {
                    clean[j] = array[i];  /* insert characters in clean array */
                    j++;                         /* move to next cell if char found */
                }                      
            }
        }
    
        clean[j] = '\0';                    /* close clean_array string */
    
    
        return (j-1);                       /* return number of characters*/
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Get rid of the inner while loop.
    Code:
       for ( i = 0; i < size; i++ )
       {
          if ( isalpha(array[i]) )
          {
             clean[j] = array[i];  /* insert characters in clean array */
             j++;                  /* move to next cell if char found */
          }
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks, so I tried to process the array within the array. Hm....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM