-1

I got two int in main.c, want to pass those two values to hello.c to run it using exce(), and return to main.c to print it, how can I do that?

I try a lot of ways but it keep fail.

main.c

    
    pid_t pid;
    pid = fork();
    int staff_id = 0210;
    int staff_working_hours = 10;

    if(0 == pid)
    {
       int args[] = {staff_id, staff_working_hours, NULL};
       excev(" ./hello.c", args);

    }
    else
    {
        
        print(" "); //print here
      
    }

    return 0;
}

hello.c

int main()
{
    //not want to print here, want to print by main.c 
    printf("Hello Staff ID: %d \n", staff_id);
    printf("Your working hours is: %d \n", staff_working_hours);
    return 0;
}
  • 2
    There is no `exec` in the code you show. Please [edit] and clarify. Also read this: [ask] and this: [mcve]. Show at least what you have tried. – Jabberwocky Apr 27 '22 at 12:21
  • 1
    Related: https://stackoverflow.com/questions/5237482/how-do-i-execute-an-external-program-within-c-code-in-linux-with-arguments – mch Apr 27 '22 at 12:21
  • 1
    "I try a lot of ways but it keep fail." --> Post you code that shows your try with `exec()`. – chux - Reinstate Monica Apr 27 '22 at 12:26
  • @chux-ReinstateMonica just add some of my failed work – lemmonlola Apr 27 '22 at 12:35
  • 3
    So is it `exce()` (quesiton) or `excev()` (code). Best to post true code. – chux - Reinstate Monica Apr 27 '22 at 12:42
  • 1
    Tip: save time. Enable all warnings. Faster good feedback than posting on SO. – chux - Reinstate Monica Apr 27 '22 at 12:43
  • Did you read the documentation of `excev`? – Jabberwocky Apr 27 '22 at 12:44
  • 2
    please read the first comment above: make a real [mcve] instead of typing dubious code like this. And if it's really `execv` why don't read the documentation of `execv`? Why pass an int array to it? – phuclv Apr 27 '22 at 12:51
  • 3
    Note that the function is normally called `execv()` — the `excev()` you refer to is not standard. Assuming that the function name is a typo, arguments are passed as an array of strings. You must use the signature `int main(int argc, char **argv)` in the second program to access the arguments passed to it. After the invocation of `execv()`, you should make sure your program reports an error and exits. If `execv()` succeeds, it does not return; if it returns, it failed. – Jonathan Leffler Apr 27 '22 at 12:53
  • 4
    Also, remember that `argv[0]` is the name of the program. Your `args` array has the wrong type (it should be `char *args[]`) and it doesn't specify the program name. Often, it is appropriate to use `execv(args[0], args)` to execute the other command. Yes, you will need to format the numbers as strings to pass them to the second program. – Jonathan Leffler Apr 27 '22 at 12:57
  • 5
    `./hello.c` probably isn't an executable file. Perhaps you meant `./hello`? – Ian Abbott Apr 27 '22 at 13:10
  • @IanAbbott want to call hello.c to input the value – lemmonlola Apr 27 '22 at 13:15
  • 3
    @lemmonlola you cannot "call" a .c file with `exec` family of funtions. You only cann "call" executables (IOW compiled programs). – Jabberwocky Apr 27 '22 at 13:17

1 Answers1

1

You can not pass a int array to execv() and expect a well defined behaviour. execv() expects a list of strings. What you need to do first is convert them to strings, or an array of strings. Something like sprintf() or since you are on Linux asprintf() could help you with that. Also, you can't execute hello.c directly, you first need to compile it and execute the compiled program. For example:

#define _GNU_SOURCE      //Needed for asprintf()
#include <stdio.h>

... //Some more code here

char command[] = "./hello";
char *nargs[4] = {command};
if(asprintf(&nargs[1],"%i",staff_id)<0) 
  { perror("asprintf"); exit(1); }
if(asprintf(&nargs[2],"%i",staff_working_hours)<0) 
  { perror("asprintf"); exit(1); }
excev(command, nargs);
perror("excv");
exit(1);

After that, you get the arguments in hello as strings, not as integers, you first have to convert them back to a int if you want to use them as int. For that strtol() could be helpful.

Note: I didn't free the memory allocated by asprintf(), here i don't need to because the program not continue to run, but don't forget that when you use asprintf() somewhere else.