char *args[MAX_ARGS];
args is an array of MAX_ARGS pointers. args itself points to the memory of the first pointer. args hold the memory address in which the first pointer is. args[0] is the value of the first pointer. args[0][0] means, that we go to the memory of the first pointer, then go to the memory address pointed to by that pointer, and then get the value of the first byte in that memory address.
char *full_command[MAX_ARGS];
Now, this is also an array of MAX_ARGS pointers. full_command points to memory region that is MAX_ARGS * sizeof(char*) bytes length. full_command[0] is the value of the first pointer inside that memory region.
Now lets try assignment:
full_command = args;
Now we get the value of the memory address of the first pointer in args and assign that value into full_command variable. The memory of full_command is lost, it's no longer accessible by any other handle. Now args[0] = smth only and only if full_command[0] = smth. full_command points to the memory region that args points to.
To copy array VALUES you need to copy each VALUE of the array:
for (size_t i = 0; i < MAX_ARGS; ++i) {
full_command[i] = args[i];
}
or using memcpy:
memcpy(full_command, args, sizeof(full_command));
After such operation full_command points to a different region that args, both are sizeof(char*) * MAX_ARGS bytes long. And they both hold the same values.
You need to assign each array value:
struct background_element bg_elem = {
.pid = child,
.number = num_background,
.full_command = { args[0], args[1], args[2], ....<up until MAX_ARGS> },
};
That's not very usuable, it needs to be modified each time MAX_ARGS changes. So use memcpy or a loop:
struct background_element bg_elem = {
.pid = child,
.number = num_background,
};
memcpy(bg_elem.full_command, args, sizeof(bg_elem.full_command));
forloop that copies each of the pointers inargsinto the corresponding location infull_command. - user3386109