user964843
user964843

Reputation:

Array as function with arguments?

I'm trying to learn c, so I tried reading some source code.
But I have no idea what this might mean:

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

The first part, static const char* is fine, as it seems to be a function (has an argument of type void), static should mean that it is only visible in this file and const char* should mean that the value cannot be changed but the address can be changed.
But in that case, it doesn't make sense after the last part following the function name, as it 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 functions ram and date? Some alternative way of declaring arrays?

Upvotes: 5

Views: 184

Answers (4)

Complex variable declarations need to be read inside out in C:

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

  • functab[] it is an array...

  • *const functab[] of constant pointers...

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

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

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

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

The = { ram, date } initialize the array with two members. Both ram and date should be functions that are 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]();

Upvotes: 0

Yogi Jason
Yogi Jason

Reputation: 121

Short answer: Here functab is an array of function pointers, and the array is initialized with pointers to functions ram and date. This also explains the name functab which is likely from "FUNCtion TABle".

Long answer: In C you can get a pointer to a function and save it to a variable. A variable used in this fashion is known as a function pointer.

For example, funcptr variable below will contain the address of do_stuff's entry point:

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 of funcptr, which is ftype_t here. The type definition should take this form:

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

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

If you didn't want to typedef only for this, you could have achieved the same thing by doing below:

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

This works, but its syntax is confusing. Also it gets quite ugly if you attempt to do something like building an array of function pointers, which is exactly what your code does.

Shown below is equivalent code, which is much easier to understand:

typedef static const char*(*myfn_t)(void);

const myfn_t functab[] = { &ram, &date };

(The & (address of) is usually optional, but recommended. )

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

functab is an array of function pointers (array of const function pointers, to be exact), which returns const char* and accepts no arguments.

Later,

 ... = { ram, date };

is a brace-enclosed initializer list which serves as the initializer for the array.

Upvotes: 3

Dipti Shiralkar
Dipti Shiralkar

Reputation: 580

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

Below discussion has good examples of array of function pointer: How can I use an array of function pointers?

Upvotes: 2

Related Questions