Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about static function pointer in c

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);
  }
}

While compiling,it complains,

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

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

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.

This code is from sis package here

like image 214
Dilawar Avatar asked Jul 29 '12 05:07

Dilawar


3 Answers

At least for GCC it will give the "invalid storage class for function" if there is a broken declaration in an included file. You may want to go back to your header files and look for what was intended to be a declaration and instead is a hanging function such as in included xxx.h file:

void foo(int stuff){       <<<<<<<<< this is the problem, replace { with ;
void bar(uint other stuff);

the open "{" really confuses GCC and will throw random errors latter. It is real easy to do a copy and paste of a function and forget to replace the { with a ;
Especially, if you use my beloved 1TBS

like image 180
MountainLogic Avatar answered Nov 23 '22 19:11

MountainLogic


I faced the same problem as the error message keep on say that:

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

The Simple Fix to this problem is "there is some brace missing in the file from which I am getting these errors."

Just go through the below example.

For example:

#include<stdio.h>
int test1()
{
    int a = 2;
    if ( a == 10)
    {
    }

will give

test.c:7: error: expected declaration or statement at end of input

Following the "invalid storage class for function" errors.

So.. Just keeping a watch on the braces could probably resolve this error.

like image 44
user3140263 Avatar answered Nov 23 '22 19:11

user3140263


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.

Does the programmer intended to write a function pointer (static)?

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.


* Actually you can as a GCC extension

like image 31
cnicutar Avatar answered Nov 23 '22 17:11

cnicutar