Hi,
In the below C code , i want to pass the array to a unix shel script.
my script should called as ex myscript 1,2,3
#include <stdio.h>
int main()
{
int a[2]={1,2,3};
}
Thanks,
Arun
Hi,
In the below C code , i want to pass the array to a unix shel script.
my script should called as ex myscript 1,2,3
#include <stdio.h>
int main()
{
int a[2]={1,2,3};
}
Thanks,
Arun
Code tags for code please.
```text
stuff
```
without the extra spaces in the tags.
Your array is too big. You specify that it has two elements but give it three initializers. This probably produces a warning.
To do what you want you need to convert it into a string. If it will always just have three initializers it could probably be made simpler but this should work for any size of array.
#include <stdio.h>
// for execlp()
#include <unistd.h>
int main()
{
const char *script="echo";
const char *prefix="";
int pos=0, n;
char buf[256];
int a[3]={1,2,3};
for(n=0; n<3; n++)
{
pos+=snprintf(buf+pos, 256-pos, "%s%d", prefix, a[n]);
}
printf("will call %s with %s\n", script, buf);
// Note that this command never returns unless it fails to run at all.
execlp(script, script, buf, NULL);
// If the program is still running, then execlp failed, so return error
return(1);
}