Luke
Luke

Reputation: 3

Why isn't my check for a null pointer working in C?

I am writing a simple function for my program in C that will print a usage statement and exit the program if called. I want to have the option to pass a string as the argument and have it displayed, but also to pass nothing and skip the line. My code is as follows with an example main function:

#include <stdlib.h>
#include <stdio.h>

typedef struct opt_info
{
    char *start_path;                  /* Path to directory to start find in */
    int  name_flag;                            /* flag for the name argument */
    int  type_flag;                            /* flag for the type argument */
} opt_info;

/* function declarations */
int   process_args( opt_info *opts, int ac, char *av[] );
void  init_params( opt_info *opts );
void  print_usage();

int main ( int ac, char *av[] ) 
{
    opt_info opts;                            /* struct to store run options */
    process_args( &opts, ac, av );         /* handle the argument processing */

    printf("%s\n", opts.start_path);

    return 0;
}

int process_args( opt_info *opts, int ac, char* av[] )
{
    if ( ac < 2 )                                         /* not enough args */   
        print_usage();

    init_params( opts );                                  /* initialize opts */
    opts->start_path = av[1];                                  /* store path */

    return 0;
}

void init_params( opt_info *opts )
{
    opts->name_flag = opts->type_flag = 0;            /* set type flags to 0 */
}

void print_usage( char *message )
{
    if( message != NULL )           /* check for empty message string */
        printf( "%s\n", message );

    printf( "USAGE: pfind starting_path [-name filename-or-pattern] " );
    printf( "[-type {f|d|b|c|p|l|s}]\n" );

    exit( 1 );
}

When I compile with gcc and run the function without passing an argument (i.e. calling print_usage();) the if statement still evaluates as true and I get a line printed out with some garbage data before the "USAGE:..." is printed.

Is there something about C pointers I am missing here, and is there a better way to have this functionality?

edit: updated code to have same function calls as the code I'm having issues with, as requested. The strange thing is when I took away some of the unnecessary code (such as the call to process_args()) I did end up with a compiler warning, but this compiles on my system fine.

Upvotes: 0

Views: 656

Answers (1)

Crashworks
Crashworks

Reputation: 41482

Pointers are not initialized to NULL by default. In fact, no C variable is initialized until you assign a value to it.

Before you assign a value to any C variable, it exists in an undefined state, typically called a "garbage value". This is whatever happened to be at that memory location already, and is typically not 0 (or NULL).

To properly initialize message when calling print_usage(), simply pass it as a parameter:

print_usage(NULL);

Upvotes: 6

Related Questions