Question: I am programming in C in terminal using VIM in Ubantu Linux. I wrote this else statement. When I build I get errors, which don't
I am programming in C in terminal using VIM in Ubantu Linux. I wrote this else statement. When I build I get errors, which don't exist if I comment the else statement out.
else {
rc = fork();
if (rc > 0) { // I am the parent printf("Parent: waiting on pid %d ", rc); waitpid(rc, &status, 0); printf("Parent: child has ended with status %d ", WEXITSTATUS(status)); exit(0); } else if (rc == 0) { // I am the child printf("Child: I am pid %d about to exec() %s ", getpid(), cmd); execvp(cmd, myArgv); perror("Child: execvp() failed"); printf("Child: execvp() failed...exiting "); exit(1); } else { // an error occured, no child created perror("Parent: a bad thing happened:"); exit(1); } }
I get the following errors when I make the file:
nanosh.c: In function main: nanosh.c:96:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion] rc = fork(); ^ nanosh.c:99:11: warning: format %d expects argument of type int, but argument 2 has type char * [-Wformat=] printf("Parent: waiting on pid %d ", rc); ^ nanosh.c:100:17: error: status undeclared (first use in this function) waitpid(rc, &status, 0); ^ nanosh.c:100:17: note: each undeclared identifier is reported only once for each function it appears in nanosh.c:100:12: warning: passing argument 1 of waitpid makes integer from pointer without a cast [-Wint-conversion] waitpid(rc, &status, 0); ^ In file included from nanosh.c:12:0: /usr/include/x86_64-linux-gnu/sys/wait.h:125:16: note: expected __pid_t {aka int} but argument is of type char * extern __pid_t waitpid (__pid_t __pid, int *__stat_loc, int __options); ^ Makefile:2: recipe for target 'nanosh' failed make: *** [nanosh] Error 1
I need you to fix the errors for me.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
