Sam P
Sam P

Reputation: 453

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

PART 1) execvp(2) takes 2 arguments (obv), the first being the command, and the second being an array of strings such as

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)?

PART 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].

Upvotes: 0

Views: 855

Answers (2)

Maksim Skurydzin
Maksim Skurydzin

Reputation: 10541

This declaration char *args[] can be deciphered as "args is an array of pointers to char". This means that each entry of an array is pointing to some location where one or more chars are located.

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).

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. The array args contains the pointers to this locations. 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). 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. 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.

Upvotes: 1

askmish
askmish

Reputation: 6684

PART 1)

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

The above code means, you are creating an array of char pointers.

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.

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

PART 2) You cannot do that, without allocating memory explicitly using malloc or calloc to the char pointers. And then use strncpy to copy string to that char pointer in the array.

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.

Upvotes: 1

Related Questions