qc.hlp (Table of Contents; Topic list)
ASSERT.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* ASSERT.C illustrates:
 *      assert
 */
 
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <assert.h>
 
#define MAXSTR 120
 
void chkstr( char *string );    /* Prototype */
 
void main()
{
    char string1[MAXSTR], string2[MAXSTR];
 
    /* Do various processes on strings and check the results. If
     * none cause errors, force an error with an empty string.
     */
    printf( "Enter a string: " );
    gets( string1 );
    chkstr( string1 );
 
    printf( "Enter another string: " );
    gets( string2 );
    chkstr( string2 );
 
    strcat( string1, string2 );
    chkstr( string1 );
    printf( "string1 + string2 = %s\n", string1 );
 
    chkstr( "" );
    printf( "You'll never get here\n" );
}
 
/* Tests a string to see if it is NULL, empty, or longer than MAXSTR. */
void chkstr( char *string )
{
    assert( string != NULL );               /* Cannot be NULL           */
    assert( *string != '\0' );              /* Cannot be empty          */
    assert( strlen( string ) < MAXSTR );    /* Length less than maximum */
}