I am programming in C in terminal using VIM in Ubantu Linux. I wrote this else s
ID: 3879106 • Letter: I
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 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.
Explanation / Answer
nanosh.c:96:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
rc = fork();
^
Answer :
You are getting the error because fork() function returns a char,char is a form of integer in C. so you are assigning integer into a char[] which is a pointer , becauseof this you are getting warning.
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
printf("Parent: waiting on pid %d ", rc);
^
Answer : you can't pass char * as a input to format '%d' as it expects integer
error: ‘status’ undeclared (first use in this function)
waitpid(rc, &status, 0);
^
Answer : status is not declared in the program, before using variable please declare it.
warning: passing argument 1 of ‘waitpid’ makes integer from pointer without a cast [-Wint-conversion]
waitpid(rc, &status, 0);
Answer : waitpid() function is expects first argument as int , but you are trying to pass it as pointer.
Please provide complete program so that we can answer completely.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.