Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 26, 2014 C style function pointers | ||||
---|---|---|---|---|
| ||||
I am trying top port this code : float interpolate( float from, float to, float amount, float (*easing)(float) ) { return from + ( to-from )*( easing( amount ) ); } float linear_interpolation( float p ) { return p; } the "float (*easing)(float) " part needs to be rewritten as "float function(float) easing" as far as I know and could find here http://dlang.org/deprecate.html#C-style function pointers. so my code is written as: import std.stdio; float interpolate(float from, float to, float amount, float function(float) easing) { return from + (to - from) * (easing(amount)); } float lineair_interpolation(float p) { return p; } void main() { writeln(interpolate(100,100,10, lineair_interpolation)); } this errors witha : Error: function test.lineair_interpolation (float p) is not callable using argument types (), but I don't really know where to go from here. |
August 26, 2014 Re: C style function pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to nikki | On Tuesday, 26 August 2014 at 12:26:45 UTC, nikki wrote: > void main() > { > writeln(interpolate(100,100,10, lineair_interpolation)); > } > > this errors witha : Error: function test.lineair_interpolation > (float p) is not callable using argument types (), but I don't > really know where to go from here. You need to put an `&` before the function name: writeln(interpolate(100,100,10, &lineair_interpolation)); |
August 26, 2014 Re: C style function pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to nikki Attachments: | On Tue, 26 Aug 2014 12:26:43 +0000 nikki via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > this errors witha : Error: function test.lineair_interpolation > (float p) is not callable using argument types (), but I don't > really know where to go from here. you need to use '&' to get function pointer. i.e. writeln(interpolate(100,100,10, &lineair_interpolation)); |
August 26, 2014 Re: C style function pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | thanks, that worked, I need to grow a feeling for those * and & |
Copyright © 1999-2021 by the D Language Foundation