Does this look right?
Program compiles fine with gcc and runs fine too, but the command[3] still bothers me. Isn't command supposed to be two char pointers?
I tried a similar program on VS2010 and it gave me runtime errors - specifically: variable arr was corrupted.
Which leads me to believe the first code was indeed incorrect. But that's how the TAs taught it to us..
And if the first piece of code is indeed wrong, how do I set it up to pass it to execvp?
Code:
int main(int argc, char** argv, char** envp)
{
int status;
int pid = fork();
char* command[2];
command[0] = "cat";
command[1] = "project1a.c";
command[2] = NULL;
if(pid == 0)
{
printf("Child %i is executing the command\n\n", getpid());
execvp(command[0], command);
}
else
{ /* In parent process */
waitpid(pid, &status, WUNTRACED);
printf("\nParent %i has finished waiting for its child process to complete\n\n", getpid());
}
return 0;
}
Program compiles fine with gcc and runs fine too, but the command[3] still bothers me. Isn't command supposed to be two char pointers?
I tried a similar program on VS2010 and it gave me runtime errors - specifically: variable arr was corrupted.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv, char** envp)
{
char* arr[3];
int i = 0;
arr[0] = "test";
arr[1] = "test2";
arr[2] = "testing";
arr[3] = NULL;
for(; i < 3; ++i)
printf("%s\n", arr[i]);
return 0;
}
Which leads me to believe the first code was indeed incorrect. But that's how the TAs taught it to us..
And if the first piece of code is indeed wrong, how do I set it up to pass it to execvp?