I need to pass two args to a shell script, here is the code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char *script;
int lines = 1;
sprintf(script, "/...path.../line.sh %d %d", lines, lines);
system(script);
}
The script works well, ive tried. But I always get Segmentation fault. The question is: why?
Thanks
You are writing to the memory location pointed to by script
which hasn't been allocated any memory.
Try something like:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char script[100]; // Allocate as much as you need here for your string, not
int lines = 1; // necessarily 100
sprintf(script, "/...path.../line.sh %d %d", lines, lines);
system(script);
}
You need to allocate space for script
char *script = malloc(/* string size */);
To be able to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With