关于c中静态函数指针的困惑

Dil*_*war 5 static function-pointers

请查看以下代码段.它写于2005年,但我用最新的gcc编译它.

xln_merge_nodes_without_lindo(coeff, cand_node_array, match1_array, match2_array)
  sm_matrix *coeff;
  array_t *cand_node_array, *match1_array, *match2_array;
{ 
  node_t *n1, *n2;
  sm_row *row1, *row2;
  static sm_row *xln_merge_find_neighbor_of_row1_with_minimum_neighbors();

  while (TRUE) {
      row1 = sm_shortest_row(coeff);
      if (row1 == NIL (sm_row)) return;
      n1 = array_fetch(node_t *, cand_node_array, row1->row_num);
      row2 = xln_merge_find_neighbor_of_row1_with_minimum_neighbors(row1, coeff);
      n2 = array_fetch(node_t *, cand_node_array, row2->row_num);
      array_insert_last(node_t *, match1_array, n1);
      array_insert_last(node_t *, match2_array, n2);
      xln_merge_update_neighbor_info(coeff, row1, row2);
  }
}
Run Code Online (Sandbox Code Playgroud)

在编译时,它抱怨,

xln_merge.c:299:18: error: invalid storage class for function ‘xln_merge_find_neighbor_of_row1_with_minimum_neighbors’
Run Code Online (Sandbox Code Playgroud)

(xln_merger.c:299是定义开始后的第3行).

第3行函数定义似乎是一个函数声明(不是吗???).程序员是否打算编写函数指针(静态)?或者某些语法在c中已经发生了变化,为什么这不是编译.

此代码来自此处的sis

小智 9

至少对于GCC,如果包含的文件中存在损坏的声明,它将给出"函数的无效存储类".您可能希望返回头文件并查找要作为声明的内容,而不是挂起函数,例如包含在xxx.h文件中:

void foo(int stuff){       <<<<<<<<< this is the problem, replace { with ;
void bar(uint other stuff);
Run Code Online (Sandbox Code Playgroud)

开放的"{"确实让GCC感到困惑,并且后来会抛出随机错误.复制和复制函数真的很容易,忘了替换{with a;
特别是,如果你使用我心爱的 1TBS


小智 9

我遇到了同样的问题,因为错误信息继续说:

libvlc.c:507:11: warning: “/*” within comment
libvlc.c:2154: error: invalid storage class for function ‘AddIntfInternal’
libvlc.c:2214: error: invalid storage class for function ‘SetLanguage’
libvlc.c:2281: error: invalid storage class for function ‘GetFilenames’
libvlc.c:2331: error: invalid storage class for function ‘Help’
libvlc.c:2363: error: invalid storage class for function ‘Usage’
libvlc.c:2647: error: invalid storage class for function ‘ListModules’
libvlc.c:2694: error: invalid storage class for function ‘Version’
libvlc.c:2773: error: invalid storage class for function ‘ConsoleWidth’
libvlc.c:2808: error: invalid storage class for function ‘VerboseCallback’
libvlc.c:2824: error: invalid storage class for function ‘InitDeviceValues’
libvlc.c:2910: error: expected declaration or statement at end of input
Run Code Online (Sandbox Code Playgroud)

对此问题的简单修复是"文件中缺少一些大括号,我从中获得了这些错误."

请看下面的例子.

例如:

#include<stdio.h>
int test1()
{
    int a = 2;
    if ( a == 10)
    {
    }
Run Code Online (Sandbox Code Playgroud)

会给

test.c:7: error: expected declaration or statement at end of input
Run Code Online (Sandbox Code Playgroud)

遵循"无效的存储类功能"错误.

所以..只需要监视大括号就可以解决这个错误.


cni*_*tar 2

尽管不常见,但在另一个函数中声明一个函数是完全有效且标准的。然而,静态修饰符在没有主体的声明中没有任何意义,并且您不能*在另一个函数中定义您的函数。

程序员是否打算编写函数指针(静态)?

我不知道原始程序员的意图,但它绝不可能是函数指针,因为没有对其进行赋值。


* 实际上你可以作为 GCC 扩展