简体   繁体   English

c字符串操作传递给execvp(2)

[英]c string manipulation to pass to execvp(2)

This may be because I'm new to C programming, but if I recall my lecturer correctly 这可能是因为我是C编程的新手,但如果我正确地回忆起我的讲师

PART 1) execvp(2) takes 2 arguments (obv), the first being the command, and the second being an array of strings such as 第1部分) execvp(2)接受2个参数(obv),第一个是命令,第二个是字符串数组,如

char *args[] = {"ls", "-l", "-a", NULL};

Can I please have an explanation of how char *args[] would make this an array of strings rather than an array with chars in it (a C null terminated string)? 我可以解释一下char *args[]如何使这个字符串数组而不是带有字符的数组(C null终止字符串)?

PART 2) 第2部分)

How can I make it so that I can add to this array string by string? 我怎样才能使它能够通过字符串添加到这个数组字符串? Could I possibly do 我可以做吗?

int i;
char *args[255];

for(i = 0; i < strlen(lol); i++)
{
    args[i] = //new string being passed in at runtime
}

and would it work like that? 它会像那样工作吗? Say I was breaking up input from stdin and I wanted to put arguments into args[i] . 假设我正在从stdin中分解输入,我想将参数放入args[i]

PART 1) 第1部分)

char *args[] = {"ls", "-l", "-a", NULL};

The above code means, you are creating an array of char pointers. 上面的代码意味着,您正在创建一个char指针数组。

Each char pointer is allocated fixed-size of memory, based on the definition of the strings within (double quotes)"" which are within (braces){} at compile-time itself. 根据(双引号)“”内的字符串的定义,每个字符指针都被分配固定大小的内存,这些字符串在编译时本身内(大括号){}。 You cannot modify the size of the strings pointed to by the char pointers in this case.You can still modify contents of the string pointed to. 在这种情况下,您无法修改char指针指向的字符串的大小。您仍然可以修改指向的字符串的内容。

Its similar to char [][4]={"ls", "-l", "-a", NULL}; 它类似于char [][4]={"ls", "-l", "-a", NULL};

PART 2) You cannot do that, without allocating memory explicitly using malloc or calloc to the char pointers. 第2部分)如果不使用malloc或calloc显式地为char指针分配内存,则不能这样做。 And then use strncpy to copy string to that char pointer in the array. 然后使用strncpy将字符串复制到数组中的char指针。

In part-1 fixed-sized memory was allocated at compile time itself.In this Part-2, it cannot be done that way since no memory has been allocated at all. 在第1部分中,固定大小的内存在编译时自行分配。在第2部分中,由于根本没有分配任何内存,所以不能这样做。

This declaration char *args[] can be deciphered as "args is an array of pointers to char". 这个声明char *args[]可以解密为“args是一个指向char的指针数组”。 This means that each entry of an array is pointing to some location where one or more char s are located. 这意味着数组的每个条目都指向一个或多个char所在的位置。

When you do a static initialization declaring args , compiler reserves the space for exactly the number of initializers you have, each having pointer to char type (in your case, there are 4 pointer in the array). 当您执行静态初始化声明args ,编译器会保留空间以确切地拥有初始化器的数量,每个初始化器都有指向char类型的指针(在您的情况下,数组中有4个指针)。

For each of the initializing strings a compiler reserves space (typically in read-only data segment) and puts individual characters there with the null being the last character. 对于每个初始化字符串,编译器保留空间(通常在只读数据段中)并将单个字符放在那里,其中null是最后一个字符。 The array args contains the pointers to this locations. 数组args包含指向此位置的指针。 Thus, you have a level of indirection there. 因此,你有一个间接的水平。

args[0] ---- points at memory location where 3 chars are -----> 'l', 's', '\0'

Regarding 2), you can do that since args as an array of pointers (and not a 2D array). 关于2),你可以这样做,因为args是指针数组(而不是2D数组)。 However you should assign the correct memory locations where the null-terminated sequences of characters are. 但是,您应该为空终止的字符序列分配正确的内存位置。 But you have to be sure that you args array is of an appropriate size. 但是你必须确保args数组的大小合适。 In case one when no size is given during array declaration, compiler allocates enough space only for the supplied initializers and it won't be possible to change the size later. 如果在数组声明期间没有给出大小,则编译器仅为提供的初始化器分配足够的空间,并且以后不可能更改大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM