【发布时间】:2025-12-30 10:20:09
【问题描述】:
我得到了一个名为 statPrint 的函数来处理系统调用 stat() 的打印。该函数由另一个 .o 文件提供。使用该函数编译我的实现时出现错误:
In function ‘main’:
statcall.c:9:19: error: expected expression before ‘,’ token
statPrint(argv[1]*,sb*);
^
statcall.c:9:19: error: incompatible type for argument 2 of ‘statPrint’
statcall.c:4:8: note: expected ‘struct stat *’ but argument is of type ‘struct stat’
extern statPrint(char*,struct stat*);
这是我的代码:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
extern statPrint(char∗,struct stat∗);
int main(int argc, char *argv[])
{
struct stat sb;
stat(argv[1],&sb); ///argv[1] contains input from the terminal/shell
statPrint(argv[1]*,sb*);
}
我用它编译(libstat包含外部函数):
gcc -o statcall statcall.c libstat.o
如何消除错误?
【问题讨论】:
-
在这一行:
extern statPrint(char∗,struct stat∗);statPrint 不应该有返回值吗?可能是int或void? -
∗看起来不像*。那是你的实际源代码??? -
您能解释一下或举个例子吗?我是 c 新手,以前从未见过 extern。 extern statPrint(char∗,struct stat∗) 是老师给我们的。我以为我应该这样声明它,然后在 main 中使用 statPrint 函数。
-
@barak 是的,我不知道为什么 * 在 * 上变得如此奇怪。
-
我实际上是想告诉你编译器不会喜欢它(如果你的代码确实是这样的话)。
标签: c gcc system system-calls