How to Create an Array of Functions

How to define an array of functions in C

I have a struct that contains a declaration like this one:

No you don't. That's a syntax error. You're looking for:

void (*functions[256])();

Which is an array of function pointers. Note, however, that void func() isn't a "function that takes no arguments and returns nothing." It is a function that takes unspecified numbers or types of arguments and returns nothing. If you want "no arguments" you need this:

void (*functions[256])(void);

In C++, void func() does mean "takes no arguments," which causes some confusion (especially since the functionality C specifies for void func() is of dubious value.)

Either way, you should typedef your function pointer. It'll make the code infinitely easier to understand, and you'll only have one chance (at the typedef) to get the syntax wrong:

typedef void (*func_type)(void);
// ...
func_type functions[256];

Anyway, you can't assign to an array, but you can initialize an array and copy the data:

static func_type functions[256] = { /* initializer */ };
memcpy(mystruct.functions, functions, sizeof(functions));

Javascript Array of Functions

var array_of_functions = [
first_function,
second_function,
third_function,
forth_function
]

and then when you want to execute a given function in the array:

array_of_functions[0]('a string');

How to declare an array of functions?

Strictly speaking you may not declare an array of functions. You may declare an array of function pointers.

It seems you mean

int (*funcs[10])(int, int);

Another way is to introduce a using declaration (or a typedef declaration) like for example

using FP = int( * )( int, int );

FP funcs[10];

or

using FUNC = int ( int, int );

FUNC * funcs[10];

Julia - generate an array of functions programmatically

When you use fill with f it sets expected type for the elements of f_array to f, in the code below I am switching to abstract type to make it possible to have any function in the array

  # create simple function just to initialize
f(x)=x

# initialize array of functions
N = 3
f_array = Array{Function}(undef, N);
f_array[1] = f;

# now we update each function
for i in 2:N
f_array[i] = x -> 3 * f_array[i-1](x)
end

print(f_array[3](2))

which produces a value of 18

How to create an array of functions in C#?

It seems to me that your for loops could be expressed like this instead:

// For Game 1
result1 = string.Join("-", StoreGame1.Select(item => item.Text));
// For Game 2
result2 = string.Join("-", StoreGame2.Select(item => item.Text));
// For Game 3
result3 = string.Join("-", StoreGame3.Select(item => item.Text));

Wouldn't that work just as well for you?

Why can't I declare an array of functions in Java?

You could try this:

public static void testFunction() {
Map<String, Function<Book, String>> mapToFunctions = new HashMap<>();

Function<Book, String> myFunction = x -> new String(x.getTitle());

mapToFunctions.put("firstfunction", myFunction);

for (Function<Book, String> f : mapToFunctions.values()) {
System.out.println(f.apply(new Book("my title")));
}
}

UPDATE:

Using Set<Function<Book, String>> it would be something like:

package com.victor.main;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

public class FunctionalTest {
public static void testFunction() {
Set<Function<Book, String>> mapToFunctions = new HashSet<>();

Function<Book, String> myFunction = x -> new String(x.getTitle());

mapToFunctions.add(myFunction);

for (Function<Book, String> f : mapToFunctions) {
System.out.println(f.apply(new Book("my title")));
}
}

public static void main(String[] args) {
testFunction();
}
}

How can I create an array of functions?

It's definitely possible, just initialize the array as:

var pending = Array<(Void -> Void)>()

or even a fancier

var pending = Array<()->()>()

or

var pending: [(Void->Void)] = []

or

var pending: [(()->())] = []

or

var penguins: [<(") <(")] = [] // kidding


Related Topics



Leave a reply



Submit