0

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

3
  • But args already is an array of strings? As for the second part check out strtok. Aug 10, 2012 at 7:01
  • Not really what I was looking for. I was looking for an explanation as to why *args[] gives an array of strings rather than a null terminated c string, and whether it's possible to dynamically insert to that array.
    – Sam P
    Aug 10, 2012 at 7:12
  • If you check e.g. the clockwise spiral rule you will know that char *args[] is an array of pointers to char, which means it's an array of strings. And no, it's not possible to add new entries to it. Then you have to use e.g. char **args and allocate the array and the strings manually. Aug 10, 2012 at 7:25

2 Answers 2

1

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.

1

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.

2
  • 1
    You can still modify contents of the string pointed to. This doesn't seem to be correct and will result in undefined behavior. (according to (C90, 6.1.4) "If the program attempts to modify a string literal of either form, the behavior is undefined") Aug 10, 2012 at 7:38
  • Btw, you can do args[3] = "foo"; in runtime for the second case, because this will only assign a pointer of location where "foo" is to args[3], which is fine. Aug 10, 2012 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.