error C2143: syntax error : missing ‘;’ before ‘type’ [closed]

It looks like you are using an older version of Visual C which does not support C99. You need to use a more modern compiler, either a current/recent version of Visual C, or ideally gcc, clang, or any modern C99-compliant compiler.

Alternatively if you are for some unfortunate reason stuck with your old version of Visual C then you could fix all such variable definitions to make them C89-compliant (i.e. move them to the start of an enclosing block).

The specific problem you are seeing is that variables are declared in the middle of a code block, rather than at the start – this has been allowed since C99 (and earlier, as an extension in compilers such as gcc). Microsoft has only recently caught up (more or less) with C99.

To fix the specific function which you are having problems with:

static void replaceSubStr ( char const * const aSource ,
                            char const * const aOldSubStr ,
                            char const * const aNewSubStr ,
                            char * const aoDestination )
{
    char const * pOccurence ;
    char const * p ;
    char const * lNewSubStr = "" ;
    int lOldSubLen ;  // <<< move variable definition to here

    if ( ! aSource )
    {
        * aoDestination = '\0' ;
        return ;
    }
    if ( ! aOldSubStr )
    {
        strcpy ( aoDestination , aSource ) ;
        return ;
    }
    if ( aNewSubStr )
    {
        lNewSubStr = aNewSubStr ; 
    }
    p = aSource ;
    lOldSubLen = strlen ( aOldSubStr ) ;  // <<< fixed line, without variable definition
    * aoDestination = '\0' ;
    while ( ( pOccurence = strstr ( p , aOldSubStr ) ) != NULL )
    {
        strncat ( aoDestination , p , pOccurence - p ) ;
        strcat ( aoDestination , lNewSubStr ) ;
        p = pOccurence + lOldSubLen ;
    }
    strcat ( aoDestination , p ) ;
}

Leave a Comment