Kale Kale - 27 days ago 4
C Question

format specifies type 'char *' but the argument has type 'char' [-Wformat]

I am attempting to check and output the command line arguments given for a specific program "format.c" when called from the terminal. I am getting the following error and I do not understand what it is telling me.
error:
format specifies type 'char *' but the
argument has type 'char' [-Wformat]
here is my code thus far:

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

int printArguments( int argc, char **argv )
{
int i;
printf( "Total number of arguments: %d", argc );
for( i = 0; i <= argc; i++ ){
printf( "argv[%d] = %s\n", i, *argv[i] );
}
return 0;
}

int main ( int argc, char **argv )
{
if( argc != 3 ){
puts( "You failed to enter the correct number of arguments." );
printf( "You entered: \nargv[0] = %s\nargv[1] = %s\nargv[2] = %s\n",
argv[0], argv[1], argv[2] );
puts( "Argument format should be, \"./format (arg1 = int value)"
"(arg2 = file name)\"\n" );
return 0;
}
if( atoi(argv[1]) < 25 ){
printf( "\nargv[1] = %d\nYou entered a value too small for the format\n",
atoi(argv[1]) );
return 0;
}
if( atoi(argv[1]) > 100 ){
printf( "\nargv[1] = %d\nYou entered a value too large for the format\n",
atoi(argv[1]) );
return 0;
}

printArguments( argc, **argv );

return 0;
} /*end of main */

Answer

Try

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

int printArguments( int argc, char **argv )
{
    int i;
    printf( "Total number of arguments: %d", argc );
    for( i = 0; i <= argc; i++ ){
        printf( "argv[%d] = %s\n", i, *(argv+i) );
    }
    return 0;
}

int main ( int argc, char **argv )
{
    if( argc != 3 ){
        puts( "You failed to enter the correct number of arguments." );
        printf( "You entered: \nargv[0] = %s\nargv[1] = %s\nargv[2] = %s\n",
               argv[0], argv[1], argv[2] );
        puts( "Argument format should be, \"./format (arg1 = int value)"
             "(arg2 = file name)\"\n" );
        return 0;
    }
    if( atoi(argv[1]) < 25 ){
        printf( "\nargv[1] = %d\nYou entered a value too small for the format\n",
        atoi(argv[1]) );
        return 0;
    }
    if( atoi(argv[1]) > 100 ){
        printf( "\nargv[1] = %d\nYou entered a value too large for the           format\n",
        atoi(argv[1]) );
        return 0;
    }

    printArguments( argc, argv );

    return 0;
} /*end of main */

Corrections are: printArguments( argc, argv ); and printf( "argv[%d] = %s\n", i, *(argv+i) );