我是StackOverflow新手,我只是想知道为什么我的C代码给了我这个错误。我真的希望这个问题得到解决,如果有人能解释为什么会发生这种情况,而不是给我答案,那将是非常感谢的。
void scanningForWS(int argc, char **argv)
{
int number = 0;
int sflag = 0;
int opt = 0;
int *s = 0;
char *getopt = 0;
char *optarg = 0;
while ((opt = *getopt(argc, argv, "w:s")) != -1) //the *getopt gives me this error
//Error: Expression much have a pointer-to function
{
switch (opt)
{
case 's':
sflag = 1;
break;
case 'w':
number = atoi(optarg);
break;
default:
break;
}
}
}这是where语句,我在需要的地方做了评论。
发现了问题,但尚未解决。我发现我没有统一,但我无法理解。有人知道我在哪里能弄到它吗?
发布于 2012-12-07 18:59:50
您正在声明一个与函数名称相同的变量,我不能说您是否包含了正确的标头。
这不是函数声明FYI,该行声明一个指向char的指针,并使用值0初始化它。当前代码唯一有意义的方法是如果getopt是一个函数指针,而它不是。
您的代码应该是:
#include <unistd.h>
void scanningForWS(int argc, char **argv)
{
int number = 0;
int sflag = 0;
int opt = 0;
int *s = 0;
/* char *getopt = 0; do not declare getopt as a variable,
just include the header uninstd.h and use it */
while ((opt = getopt(argc, argv, "w:s")) != -1)
/* ... */
}发布于 2012-12-07 19:04:02
Remove char *getopt; getopt是unistd.h中的一个函数,通过声明char指针,您正在做一些非常奇怪的事情:)
发布于 2012-12-07 18:56:36
getopt返回一个int,而不是int *。
从while循环中的使用中删除*:
while ((opt = getopt(argc, argv, "w:s")) != -1) //the *getopt gives me this error至:
while ((opt = getopt(argc, argv, "w:s")) != -1) //the *getopt gives me this errorhttps://stackoverflow.com/questions/13769246
复制相似问题