Array as a function with arguments?

I am trying to learn c, so I tried to read some source code.
But I have no idea what this could mean:

static const char*(*const functab[])(void)={ ram,date }; 

The first part, static const char* beautiful because it seems to be a function (has an argument of type void ), static should mean that it is visible only in this file, and const char* means that the value cannot be changed, but the address can be changed.
But in this case it does not make sense after the last part following the function name, as was the case with

 static const char * date(void); static const char * ram(void); 

Instead of the function name, there is (*const functab[]) , a const array called functab containing addresses?
Is this some kind of wrapping function containing the ram and date functions? Any alternative way to declare arrays?

+5
source share
4 answers

Complex variable declarations must be read inside out in C:

  • functab is a variable identifier, so we start reading here ...

  • functab[] is an array ...

  • *const functab[] constant pointers ...

  • (*const functab[])(...) to functions ...

  • (*const functab[])(void) that take no arguments ...

  • const char*(*const functab[])(void) , but return const char* .

The value of static depends on whether it is inside or outside the function. If it is outside, static means that functab declared with a file scope (i.e., a global variable that is visible only within a single .c file). If it is inside a function, this means that functab is a global variable that is visible only inside this function.

= { ram, date } initializes the array with two members. Both ram and date must be functions declared as const char* ram(void) .

The effect of this declaration is that the following function calls are equivalent:

 const char* result = ram(); const char* result = functab[0](); 
+1
source

functab - an array of function pointers (an array of const function pointers, to be precise) that returns const char* and does not accept arguments.

Further

  ... = { ram, date }; 

is a parenthesized list of initializers that serves as an initializer for an array.

+4
source

This is a way to define an array of function pointers in C. Therefore, instead of calling a function as ram (), using this array, you can call it (* functab[1]) .

There are good examples of a function pointer array below the discussion: How can I use an array of function pointers?

+3
source

Short answer: Here functab is an array of pointers to functions, and the array is initialized with pointers to functions ram and date . This also explains the name functab, which probably refers to "FUNCTION TABLE".

Long answer: in C you can get a pointer to a function and save it in a variable. The variable used in this way is called a function pointer.

For example, the funcptr variable funcptr will contain the address of the do_stuff entry do_stuff :

 int do_stuff(const char* x) { ...do stuff.. } ... ftype_t funcptr = &do_stuff; ... (*funcptr)("now"); // calls do_stuff() 

This will only work if you have already defined the type funcptr , which is located here ftype_t . The type definition should take the following form:

 typedef int (*ftype_t)(const char*); 

In English, this means that ftype_t is defined as a type of function that takes const char* as its only argument and returns int .

If you didn’t want a typedef just for this, you could achieve the same by doing the following:

 int (*funcptr)(const char*) = &do_stuff; 

This works, but its syntax is confusing. It also gets pretty ugly if you try to do something like building an array of function pointers, which is what your code does.

The following is equivalent code that is much easier to understand:

 typedef static const char*(*myfn_t)(void); const myfn_t functab[] = { &ram, &date }; 

(and (address) is usually optional but recommended.)

+2
source

Source: https://habr.com/ru/post/1261173/


All Articles