Thread: strcat() problems...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    strcat() problems...

    Hi all,

    My program seems to be giving me an error in the form of a segmentation fault...

    Please see my code below with a comment show the point where program execution fails.

    Please advise!

    Code:
    //////////////////////////////////////////////////////////////////////////
    // Pre-processor Directives
    //////////////////////////////////////////////////////////////////////////
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NBARGS      2
    #define FIFO_FILE   "MYFIFO"
    #define MESSAGESIZE 80
    
    //////////////////////////////////////////////////////////////////////////
    // Sub Function Prototypes
    //////////////////////////////////////////////////////////////////////////
    
    void argumentValidater( int );
    char * concatenator( int, char** );
    
    //////////////////////////////////////////////////////////////////////////
    // Main Function Implementation
    //////////////////////////////////////////////////////////////////////////
    
    int main( int argc, char *argv[] )
    {
        argumentValidater( argc );
        char *userString;
        FILE *fp;
    
        if( ( fp = fopen( FIFO_FILE, "a" ) ) == NULL ) 
        {
            perror( "fopen" );
            exit( EXIT_FAILURE );
        }
    
        userString = concatenator( argc, argv );
        printf( "\nUSERSTRING: %s", userString );fflush( stdout );
    
        fputs( userString, fp );
        fclose( fp );
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Sub Function Implementation
    //////////////////////////////////////////////////////////////////////////
    
    void argumentValidater( int argCount )
    {
        if( argCount < NBARGS )
            printf( "\nYou must enter a number followed by your string of text!  EXITING...\n" );fflush( stdout );
    }
    
    //////////////////////////////////////////////////////////////////////////
    
    char * concatenator( int argCount, char *argVector[] )
    {
        int i = 1;
        char *str;
        while( i < ( argCount + 1 ) )
        {
            char *tmpStr;
            tmpStr = argVector[i];
            strcat( str, tmpStr );  /* Problem is here... */
            strcat( str, " " );
            i++;
        }
        strcat( str, "\n" );
        return str;
    }
    
    //////////////////////////////////////////////////////////////////////////

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Let me ask you a question:
    In your function concatenator(), the object str is a pointer. Where does it point to?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ugh, after you fix what qny pointed out, there's still the thing that calling strcat in a loop like that sucks for performance because strcat has to keep finding the null character and then appending past that point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat problems
    By thundercraker in forum C Programming
    Replies: 23
    Last Post: 10-15-2010, 02:57 AM
  2. strcat() problems, maybe
    By cloudsword in forum C Programming
    Replies: 10
    Last Post: 09-13-2009, 02:52 AM
  3. strcat problems
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2004, 04:52 PM
  4. strcat problems
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 06:51 AM
  5. Strcat function problems
    By s1k3 in forum C++ Programming
    Replies: 1
    Last Post: 12-14-2001, 12:20 AM