分段故障sprintf [E]

sha*_*ggy 4 c shell

我需要将两个args传递给shell脚本,这里是代码:

#include <stdio.h>

#include <stdlib.h>

void main()
{
char *script;
int lines = 1;
sprintf(script, "/...path.../line.sh %d %d", lines, lines);
system(script);
}
Run Code Online (Sandbox Code Playgroud)

该脚本运行良好,我尝试过.但我总是得到分段错误.问题是:为什么?

谢谢

Pau*_*aul 5

您正在写入script尚未分配任何内存的内存位置.

尝试类似的东西:

#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);
}
Run Code Online (Sandbox Code Playgroud)