I've been trying to copy an array of strings called char* args[] which contains basic shell commands such as ls and sort into another array of strings char* argsNew[] and it is not working properly. below is my code for copying one string of arrays to another using strcpy
.
char *args[2]; //Let's say i had this as my original array
args[0] = "ls";
args[1] = "sort";
char *argsNew[2]; //ad don't place anything in argsNew
//shouldn't this work?
for(int k = 0; args[k] != '\0'; ++k)
{
strcpy(argsNew[k], args[k]);
printf("argsNew[%d] is %s\n", k, argsNew[l]);
}
When I execute this the printf
never executes causing me to assume that there was an error when executing the strcpy
command. Am I using strcpy
incorrectly? Is there a better and simpler way of performing such a task? Any help would truly be appreciated. Thank you.
Edit: I added some more details to the top. Thank you for your speedy responses.