use*_*230 1 c variables pointers integer
shell_command(char gcommand[100]) {
FILE *pipe = popen("ls", "r");
char output[100];
if ( pipe ) {
fgets(output, sizeof output, pipe);
pclose(pipe);
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
结果是
program.c:在函数'shell_command'中:
program.c:42:warning:return从指针生成整数而没有
强制转换program.c:42:warning:function返回局部变量的地址
我两天都用Google搜索,但没有成功
你已宣布你的职能为
shell_command(char gcommand[100])
Run Code Online (Sandbox Code Playgroud)
由编译器解释为
int shell_command(char gcommand[100])
Run Code Online (Sandbox Code Playgroud)
虽然你想要它
char* shell_command(char gcommand[100])
Run Code Online (Sandbox Code Playgroud)
这是行不通的,因为输出是一个堆栈变量,返回的是未定义的行为,正如编译器告诉你的那样.