I'm currently trying to have my C program read Unix arguments from the user. I've so far searched this site but I haven't been able to figure out exactly what I'm doing wrong - though admittedly my pointer implementation skills are rather limited.
The following is how I have the code now; I've been messing around with the pointers with no luck. The errors are also saying that I need to use const *char, but I've seen in other examples the *char can be input by the user.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
char args[128];
//User input
printf("> ");
fgets(args, 128, stdin);
execvp(args[0], *args[0]);
}
The error I get is as follows:
smallshellfile.c: In function ‘main’:
smallshellfile.c:13:21: error: invalid type argument of unary ‘*’ (have ‘int’)
smallshellfile.c:13:5: warning: passing argument 1 of ‘execvp’ makes pointer from integer without a cast [enabled by default]
/usr/include/unistd.h:575:12: note: expected ‘const char *’ but argument is of type ‘char’
Does anyone know what the problem may be?
You have several problems:
*args[0]
is meaningless. args
is array. args[0]
is char. what is *args[0]
?
You have to create a NULL-terminated array of char*
, to pass as 2nd argument.
args[0]
is the first char in args
. you should pass the whole string (just args
), not only its first char.
Try something like:
char *argv[]={args,NULL};
execvp(args,argv);