Hey I'm a newbie programmer in C and do not understand these errors I'm getting in my program. Can someone please explain to me what is wrong with my code and the way to go about fixing it. I am eager to learn this. Appreciate any help.
Compiler Message:
gcc project1shell.c
project1shell.c: In function ‘main’:
project1shell.c:55:8: warning: passing argument 1 of ‘hello’ makes pointer from integer without a cast [enabled by default]
project1shell.c:16:6: note: expected ‘char *’ but argument is of type ‘char’
project1shell.c:62:8: warning: passing argument 1 of ‘forkk’ makes pointer from integer without a cast [enabled by default]
project1shell.c:18:6: note: expected ‘char *’ but argument is of type ‘char’
My Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#define CHILD 0
#define SIZE 256
char hello(char command[SIZE]);
void forkk(char command[SIZE]);
void exitt(char command[SIZE]);
/////////////////////////////
int main(void) {
/* variable delcarations */
char command[SIZE]="";
char buffer[SIZE]="";
/////////////////////////////
/**** prompt the user with the program ****/
printf("*** Welcome to LJ's Shell! ***\n\n");
/**** implements exit and commands ****/
while(1) {
if (strcmp(command, "exit") == 0) {
break;
}
hello(command[SIZE]);
//prompt(command[SIZE], buffer[SIZE]);
printf("nshell:~$ ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%s", command);
forkk(command[SIZE]);
}
}
///////// Functions //////////
/************ fork process **************/
void forkk(char command[SIZE]) {
int pid = 0;
int childvalue = 0;
if (strcmp(command, "fork") == 0) {
pid = fork();
if (pid != CHILD) { /* this is the parent */
printf("I am the parent. Ready to wait on the child.\n");
pid = waitpid(-1, &childvalue, 0);
printf("Child %d returned a value of %x in hex.\n", pid, childvalue);
return;
}
else { /* this is the child */
printf("I am the child.\n");
exit(2);
}
}
}
/***************** exit *******************/
void exitt(char command[SIZE]) {
if (strcmp(command, "exit") == 0) {
exit(0);
}
}
/************* Hello Test ****************/
char hello(char command[SIZE]) {
if (strcmp(command, "hello") == 0) {
printf("Hello there!\n");
//reset command back to ""
}
}
Change
hello(command[SIZE]);
to
hello(command);
the same applies to forkk(command[SIZE]);
. This should solve the compile problem.
The problem is, command
is an array, while command[n]
is an element. And it's illegal to access command[SIZE]
because the array has only SIZE - 1 elements.