shell_command(char gcommand[100]) {
FILE *pipe = popen("ls", "r");
char output[100];
if ( pipe ) {
fgets(output, sizeof output, pipe);
pclose(pipe);
}
return output;
}
结果
program.c: In function ‘shell_command’:
program.c:42: warning: return makes integer from pointer without a cast
program.c:42: warning: function returns address of local variable
我用谷歌搜索了两天没有成功
最佳答案
您已将函数声明为
shell_command(char gcommand[100])
被编译器解释为
int shell_command(char gcommand[100])
如你所愿
char* shell_command(char gcommand[100])
这是行不通的,因为输出是一个堆栈变量并返回未定义的行为,正如您的编译器告诉您的那样。
关于没遇到过的C错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25328178/