Ç - 使用execvp用户输入

Ç - 使用execvp用户输入

问题描述:

我目前想有我的C程序读取用户的Unix的参数。我到目前为止搜索这个网站,但我一直无法弄清楚到底是什么,我做错了 - 但无可否认我的指针执行技能是相当有限的。

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.

下面是我怎么会有现在code;我一直在瞎搞与没有运气的指针。该错误也说,我需要使用常量*字符,但我已经在其他的例子看到的字符*可以由用户输入。

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?

您有几个问题:


  1. * ARGS [0] 是没有意义的。 ARGS 是数组。 ARGS [0] 为char。是什么 * ARGS [0]

  1. *args[0] is meaningless. args is array. args[0] is char. what is *args[0]?

您必须创建的char * 的NULL结尾数组,传递为第一个参数。

You have to create a NULL-terminated array of char*, to pass as 2nd argument.

ARGS [0] ARGS的第一个字符。你应该通过整个字符串(只是 ARGS ),不仅是它的第一个字符。

args[0] is the first char in args. you should pass the whole string (just args), not only its first char.

尝试是这样的:

char *argv[]={args,NULL};
execvp(args,argv);