【问题标题】:How to handle given extern function in C如何在C中处理给定的外部函数
【发布时间】: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 不应该有返回值吗?可能是intvoid
  • 看起来不像*。那是你的实际源代码???
  • 您能解释一下或举个例子吗?我是 c 新手,以前从未见过 extern。 extern statPrint(char∗,struct stat∗) 是老师给我们的。我以为我应该这样声明它,然后在 main 中使用 statPrint 函数。
  • @barak 是的,我不知道为什么 * 在 * 上变得如此奇怪。
  • 我实际上是想告诉你编译器不会喜欢它(如果你的代码确实是这样的话)。

标签: c gcc system system-calls


【解决方案1】:

这行没有意义:

statPrint(argv[1]*,sb*);

没有以* 结尾的有效语法。


我想你想要:
statPrint(argv[1], &sb);

建议您阅读变量和指针的地址。

【讨论】:

  • 是的,需要刷新我的 c 语法,从一开始也不太习惯。它在修复所有语法后工作。
【解决方案2】:

你的函数需要char *请提供它

statPrint(argv[1],sb);

我真的不明白argv[1]*是什么

【讨论】:

  • 忘了提到我从终端/外壳给出了一个值(路径)。你的意思是我应该在哪里提供字符?我不是用 statPrint(argv[1]*,sb*); ?
  • @nelthas 你能解释一下为什么要添加*吗?