如何将指针添加到C中的指针数组
我正在尝试在C中创建一个简单的外壳。我在使用execve()函数时遇到问题。因此,我的论点被宣布为char *cmdargs [10];持有-A或-L等论点。但是,它与我的执行函数不起作用,我认为因为此数组没有命令作为第一个参数,因此我不确定如何将命令添加到数组的第一个。 假设现在的数组是
cmdargs[] = {"-a", "-l", NULL};
我想成为数组 cmdargs [] = {“ ls”,“ -a”,“ - l”,null};
但是,该命令被声明为指针:char *cmd; 因此,我如何将此指针添加到数组的开始。
I am trying to create a simple shell in C. I am having issues with using the execve() function. So I have my arguments declared as char *cmdargs[10]; which holds the arguments such as -a or -l. However, it does not work with my execute function and I assume that because this array does not have the command as the first argument, and I am not sure how to add the command to the first of the array.
Assuming now the array is
cmdargs[] = {"-a", "-l", NULL};
I want to the array to becmdargs[] = {"ls", "-a", "-l", NULL};
However, the command is declare as a pointer: char *cmd;
so how I can add this pointer to the beginning of my array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的数组为 *cmdargs [] = {“ -a”,“ -l”,null},则可以做到这一点。
我在此处使用execv以简单:execve要求null终止的env数组作为最后一个参数
Here is how you can do if your array is *cmdargs[] = {"-a", "-l", NULL};
I used execv here for simplicity: execve ask for a null terminated env array as last argument