-5

why I am not succeed

 int main(char* name,int arg0,int arg1)
 {
   name = "/u/e2014/Desktop/os/Prog.c";
   arg0 = 0;
   arg1 = 1;

   char my_args[3];
   my_args[0] = arg0;
   my_args[1] = arg1;
   my_args[2] = NULL;
   execl(name,m_args);

   return(0);
  }

enter image description here

I want that my program will execute the program in the path "name". Right now its do nothing.

I am not understand where is my mistake? I program in C on linux, and compile it with gcc

Thanks a lot!!

LIMA
  • 125
  • 1
  • 1
  • 8
  • 1
    What on earth were you *expecting* this program to do?? – r3mainer Nov 23 '16 at 09:15
  • to execute the program in name = "/u/e2014/Desktop/os/Prog.c" – LIMA Nov 23 '16 at 09:17
  • You have the wrong declaration of `main`, and the wrong parameters for `execl`. What parameters are you supposed to pass to that other program? – Joni Nov 23 '16 at 09:24
  • path of file like: "/u/e2014/Desktop/os/Prog.c" and two integers – LIMA Nov 23 '16 at 09:24
  • 1
    There are so many problems with this code that I'm inclined to close-vote on account of it being too broad. You can't execute C code directly. It has to be compiled. You can't mess around with the signature of the `main()` function. You clearly haven't read the documentation for `execl()`.... – r3mainer Nov 23 '16 at 09:25
  • 1
    Please read [images considered harmful to S.O. postings](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) and refrain from embedding images in your Qs in the future. Good luck. – shellter Nov 23 '16 at 10:02

2 Answers2

1

gcc has 3 different signature for main function

int main(void);
int main(int argc, char* argv[]);
int main(int argc, char *argv[], char *envp[]);

Your main function doesn't match either of these. therefore compiler error.

For your case you can use the 2nd signature with a small modification.

#include <stdlib.h>

int main(int argc, char **argv)
{
    char *path;
    int int1, int2;

    path = argv[1];
    int1 = atoi(argv[2]);
    int2 = atoi(argv[3]);
}
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • ok, so now its more complicated. i want to pass to main : path of file and two integers. with this signature, i am not see how i can do that... :/ – LIMA Nov 23 '16 at 09:23
  • i need to pass two integers to main: this signature its o.k? : int main( int argc, int argc1, char **argv) because i am searching for information about this signature with 2 parameters and i am not find – LIMA Nov 23 '16 at 09:36
0

First you are passing wrong parameter in int main(). main() has at least 3 args only.

int main(int argc, char*argv[], char *envp[]);

To execute your program you should use execvp() because you passing arrey of char* not command-line arguments via a variable-argument.

difference between execl and execv?

**L vs V: whether you want to pass the parameters to the exec'ed program as

L: individual parameters in the call (variable argument list): execl(), execle(), execlp(), and execlpe() V: as an array of char* execv(), execve(), execvp(), and execvpe()**

#include <stdio.h>
#include <unistd.h>

int main(int argc,char*argv[])
 {
   char *name = "/root/a.out";
   char *arg0 = "0";
   char *arg1 = "1";

   char *my_args[4];

   my_args[0] = name;
   my_args[1] = arg0;
   my_args[2] = arg1;
   my_args[3] = NULL;
  execvp(my_args[0],my_args);

   return(0);
  }
Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19
  • this signature will compiled: int main(int argc, int argc1, char*argv[]) because i need two integers – LIMA Nov 23 '16 at 09:39
  • @LilachDery no it will not work. if you want to get integer then you have to convert int from **argv as samuel Robert did in 2 answer of your question. – Sumit Gemini Nov 23 '16 at 09:42