function prototype with multiple parameters including void - void-pointers

I have a piece of software in which there is a function ie:
void function_name(structure_t *param1, void *param2){code....}
I am trying to create a function prototype for this function so it can be linked to another function that occurs before it. I have tried the below line with no success, it does not want to compile.
void function_name(structure_t, void);
I have this line below the associated structure but my guess is the problem is related to the void. The function itself takes the void *param2, which to be honest, confuses me but it works.
The compiler gives the error message: "'void' must be the only parameter"

The Function declaration is missing the * to define the arguments as pointers!
void function_name(structure_t *, void *);

Related

Dart method types doing odd things

I have the following Dart code:
void main() {
provide(1, 'A');
}
void provide<A>(A one, A two) {
print('one $one two $two');
}
In java the call to provide will give a compile time error as the two parameters should be both of type A. With java as soon as you pass an argument to a typed parameter that defines the type.
My understanding is that with Dart if I don't follow 'provide' with a type then the type is dynamic.
To get the above code to work correctly in dart I have to write:
void main() {
provide<int>(1, 'A');
}
void provide<A>(A one, A two) {
print('one $one two $two');
}
This will now give a compile type error as 'A' is not an int.
This however is error prone as the user is likely to forget to add the type when callng provide.
Is there anyway I can make calls to the provide method type safe without having to write provide<int>(....
I've trivialised the example, the aim is to make a call to a method such as :
void provide(Token<T> token, T value);
If T is not the correct type then the user should get a compile error.

Platform Invoke F# callback functions

I am using F# on a Raspberry Pi 2 (ARM 7 & thus mono). I am currently trying to use the WiringPi library, written in C. I have successfully managed to use some of the functions using P/Invoke.
Now I am trying to use interrupts (see http://wiringpi.com/reference/priority-interrupts-and-threads/) but I am stumped by this function with the following C signature
int wiringPiISR (int pin, int edgeType, void (*function)(void));
Which has been translated (see https://github.com/danriches/WiringPi.Net/blob/master/WiringPi/WrapperClass.cs) by Daniel Riches into a C# library like this
//This is the C# equivelant to "void (*function)(void))" required by wiringPi to define a callback method
public delegate void ISRCallback();
[DllImport("libwiringPi.so", EntryPoint = "wiringPiISR")]
public static extern int wiringPiISR(int pin, int mode, ISRCallback method);
How on earth would I do this in F#? I guess DllImport line looks like this ("method" is reserved in F#)
[<DllImport("libwiringPi.so", EntryPoint = "wiringPiISR")>]
extern int wiringPiISR(int pin, int mode, ISRCallback callBack);
What does the type definition for ISRCallback look like?
Note: this is not just "a" function pointer but a void one, with void arguments.
The delegate definition would look something like this:
type ISRCallback = delegate of unit -> unit
And the platform invoke signature would look like this:
[<DllImport("libwiringPi.so", EntryPoint = "wiringPiISR")>]
extern int wiringPiISR(int pin, int mode, [<MarshalAs(UnmanagedType.FunctionPtr)>]ISRCallback callBack);
An example usage of the function:
let callback : ISRCallback = ISRCallback(fun () -> (*do something interesting here*) ())
let result = wiringPiISR(1, 1, callback)
You should keep in mind that delegates in .NET are subject to garbage collection. It is the responsibility of the developer to ensure that the delegate doesn't "go out of scope" until your native library no longer needs the function callback.

Understanding complex block syntax

I'm a beginner to Objective C and iOS development, but a 13-year .NET veteran. I'm having a hard time mentally diagramming the following declaration, which came from the Programming with Objective C guide:
void (^(^a)(void (^) (void))) (void) = ...
It's used as an example of why one should use typedef to define blocks, but I want to understand what I'm looking at to better get a feel for block definition syntax in the first place.
Here's how I've diagrammed it so far:
Where I'm running into problems is that here is how I understand the basic syntax:
[return_val] (^[block_name]) ([block_args]) = ...
If that's the case, then what I have is a block that returns void and has no arguments, but is named (^a) (void (^) void). Meaning the name of my block, rather than being a straight string, is itself a block.
Clearly I'm missing something here. Can someone please shed some light on it? According to the site, it simplifies to this:
typedef void (^SimpleBlock) (void);
SimpleBlock (^complexBlock) (SimpleBlock) = ...
I'm just missing how.
Edit: The third void should have been in parentheses. I fixed that. It's wrong in the image, but I didn't feel like redoing the entire image just for that. :) If it turns out to be the source of my problem, I will fix it here.
In your example you're missing some parentheses for the third void
void (^(^a)(void (^)(void)))(void)
Now let's break it down. The basic syntax to return a block from a function is:
void (^f())(void) {
return ^{};
}
In this example, the returned block takes no arguments and returns void.
Now let's build your example.
void (^myBlock)(void); // Block returning void, taking no args
void (^myBlock)(void (^)(void)); // Block returning void, taking block as arg
int (^myBlock)(void (^)(void)); // Block returning int, taking block as arg
void (^ (^myBlock)(void (^)(void)) )(void); // Block returning block, taking block as arg
I've aligned the central part in each line to make it easier to read. So the difficult part seems to be returning a block. In the last line we used the syntax I described earlier to return a block from a function.
Obviously the typedefs make it much easier to read.
EDIT:
Consider this example where, in the first line, i replace int for a block with the intuitive return syntax:
void (^ )(void) (^myBlock)(void (^)(void)); // Syntax we 'intuitively would use'
void (^ (^myBlock)(void (^)(void)) )(void); // Official syntax
I'm not 100% sure of what I'm about to say, but my suspicion is that the reason for this weird syntax is so that the parser in the compiler doesn't get confused. The first 'intuitive' syntax would make the compiler think that we have a block taking no arguments returning void, and the remaining characters would be considered syntax error.
In my opinion, syntax is something you don't question too much (you can criticize it, of course) because it's part of the design on a language, and we have to follow the rules (set by some hopefully smart engineers) for our code to compile.
void (^(^a)(void (^) (void))) (void)
Break these syntax in several pieces:
a is a variable.
it can be dereferenced like c pointer "*" by the syntax "^" : ^a.
(^a)(void (^) (void) is a block named a and takes a block (void (^) (void) as parameter.
it's return value can be dereferenced to yield a block information : ^(^a)(void (^) (void)). (and by implication the return value is therefore a block pointer)
this returned block take no parameter : (^(^a)(void (^) (void))) (void).
and this returned block doesn't need return value : void (^(^a)(void (^) (void))) (void)
So let's say (^a) (void (^) void) is not Meaning the name of my block, rather than being a straight string, is itself a block.. The block literal doesn't have to be
[return_val] (^[block_name]) ([block_args])
the complier will take the code after a caret as a block.

Metatrader MQL4: Can't define function default values in .mqh file

I can't understand how to define default values for functions in my library. Default values tend to be ignored and I get "wrong parameters count" error message.
Here is my example. I created simple test library experts\libraries\test.mq4:
void test(int i = 0) // Note the default value for "i"
{
}
Then I created .mqh file as experts\include\test.mqh:
#import "test.ex4"
void test(int i = 0); // Note the default value for "i"
#import
Now I create simple expert "experts\simpletest.mq4":
#include <test.mqh>
int start()
{
// Should be able to call test() function without providing any arguments,
// because it has default value.
// If I change this line to test(0), everything compiles correctly
test(); // Causes "wrong parameters count" compilation error
return(0);
}
And I get the following error for test() function call:
')' - wrong parameters count
If I change this function call to test(0), everything compiles, but I should be able to call test() function without providing any parameters, because I have default value for first parameter in .mqh file, like this: void test(int i = 0);
Why it doesn't use the default value?
I search google for any clue, but can't find any references about this problem. Anybody knows?
This is not possible as stated in the MQL Documentation:
MQL4-library functions imported within other modules cannot have parameters initialized by default values.

What does this error in calling a method mean?

I have just started out in Vala, and I tried to make a simple program that asks two inputs:
An int that specifies a cycle degree; and
A char that contains I / R for either an iterative or recursive process.
Just before compiling, I got this error:
test0.vala:8.5-8.16: error: Access to instance member `test0.test_exec' denied
test_exec(q);
^^^^^^^^^^^ //the entire statement
Compilation failed: 1 error(s), 0 warning(s)
The pastebin for the very simple program is located here.
Here's a snippet:
public static void main(string[] args)
{
stdout.printf("Greetings! How many cycles would you like? INPUT: ");
int q=0;
stdin.scanf("%d", out q);
test_exec(q);
}
public void test_exec(int q)
{
//method code here
}
Can you please enlighten me about what to do, and some tips? Thanks.
You defined test_exec as an instance (non-static) method. Unlike a static method, an instance method needs to be called on an instance of the given class. However you're trying to call it without such an instance and thus get an error.
So you either need to create an instance of the test0 class and call test_exec on that (though that would make little sense since test_exec does not depend on or change any state of the object - as a matter of fact the test0 class does not have any state) or make test_exec as well as the other methods called by test_exec static.

Resources