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

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

本文介绍了关于C中静态函数指针的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Look at the the following code snippet. It was written in 2005 but I am compiling it with latest 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);
  }
}

编译时会抱怨

xln_merge.c:299:18: error: invalid storage class for function ‘xln_merge_find_neighbor_of_row1_with_minimum_neighbors’

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

(xln_merger.c:299 is line 3 here after definition starts).

第3行函数定义似乎是一个函数声明(不是吗???).程序员是否打算编写函数指针(静态)?或随着时间的流逝,某些语法已经更改,为什么不进行编译.

Line 3 function definition seems to be a function declaration (isn't it???). Does the programmer intended to write a function pointer (static)? Or some syntax has changed over the time in c why this is not compiling.

此代码来自sis此处

推荐答案

尽管不常见,但在另一个函数中声明一个函数是完全有效和标准的.但是,在没有主体的声明中,static修饰符毫无意义,并且您不能*在另一个函数中定义函数.

Though uncommon, it's completely valid and standard to declare a function inside another one. However the static modifier makes no sense in a declaration without a body, and you can't* define your function inside another function.

我不知道原始程序员的意图,但由于没有赋值,所以它绝对不能成为函数指针.

I can't know the original programmer's intentions but in no way can that be a function pointer since there's no assignment to it.

*实际上,您可以作为GCC扩展名

* Actually you can as a GCC extension

这篇关于关于C中静态函数指针的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:51