I'm attempting to split the string in "tmp" and put it into the array of pointers "arr". I'm doing this because I intend to used "execv" on "arr" but I can't because "arr" does not have NULL at the end. Instead it has "(null)" and when I print it's contents I get a segmentation fault. How would I set "arr" correctly so that it can be used with execv? Thanks
#include<stdio.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<string.h>
int main(){
char tmp[40] = "echo Hello World \n ";
char *arr[40];
char *token = strtok(tmp, " \n");
int index = 0;
while(token != NULL){
arr[index] = malloc(strlen(token) + 1);
arr[index] = token;
index = index + 1;
token = strtok(NULL, " \n");
}
for(int i = 0; i < 40; i++){
printf("%s \n", arr[i], arr[i]);
}
return 0;
}