Mona Jalal
Mona Jalal

Reputation: 38135

expected ‘char **’ but argument is of type ‘char *’ --argv

I am trying to create a simple shell which takes something like "ls" or "ls -l" and executes it for me.

Here's my code:

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h> 
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

void execute(char **argv)
{
  int status;
  int pid = fork();
  if ((pid = fork()) <0)
     {
       perror("Can't fork a child process\n");
       exit(EXIT_FAILURE);
     }
  if (pid==0)
     {
    execvp(argv[0],argv);
        perror("error");
     }
  else
     {
      while(wait(&status)!=pid) 
         ;
     }


}

int main (int argc, char **argv)

{

   char args[256];
   while (1)
   {
      printf("shell>");
      fgets(args,256,stdin);
      if (strcmp(argv[0], "exit")==0)
             exit(EXIT_FAILURE);
      execute(args);
   }

}

I am receiving the following error:

basic_shell.c: In function ‘main’:
basic_shell.c:42: warning: passing argument 1 of ‘execute’ from incompatible pointer type
basic_shell.c:8: note: expected ‘char **’ but argument is of type ‘char *’

Can you please give me some pointers on how to correctly pass the arguments to my execute function?

Upvotes: 0

Views: 8799

Answers (2)

P0W
P0W

Reputation: 47784

note: expected ‘char **’ but argument is of type ‘char *’ says all.

What else you need ?

args is decaying to char * but you have char ** for void execute(char **argv)

You need to split your args into

  • command
  • options

Use strtok function

Upvotes: 2

Carl Norum
Carl Norum

Reputation: 224844

Right now, you're passing a single string to execute(), which is expecting an array of strings. You'll need to break up args into the component arguments so that execute() can handle them properly. strtok(3) is probably a good place to start.

Upvotes: 2

Related Questions