The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 passing a *function to another function

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
lilo Posted - 12/05/2002 : 09:37:31 AM
How can I pass a pointer to a function to another function
Here's some code from numerical recipes
void mrqcof([..other args..], void (*funcs)(float, float, float [], float *, float [], int)){
[..]
(*funcs)(x[i],dfx[dfi],a,&ymod,dyda,ma);
[..]
}

This works fine with gcc but produces the following Compiler error message in Origin:
Error, illegal function argument type
for the 1st line cited here. Does anyone know a fix?

Edited by - lilo on 12/05/2002 09:38:32 AM

Edited by - lilo on 12/05/2002 09:39:56 AM
8   L A T E S T    R E P L I E S    (Newest First)
ML Posted - 12/09/2002 : 6:05:48 PM
The void return crash issue will be taken care of in the upcoming service release.
easwar Posted - 12/09/2002 : 2:24:48 PM
Hi Dave,

Thank you for reporting this bug. It has been trackred to be fixed in a future service release.

Easwar
OriginLab.
Barb Tobias Posted - 12/09/2002 : 1:49:26 PM
Hi Dave,
There were quite a few Origin C improvements in SR2. Check out the release notes.
-Barb

dlw21 Posted - 12/09/2002 : 12:16:19 PM
Easwar,

This is very interesting. I have 7SR2, but was unaware that this feature had been added.

I was able to get your example to compile and run just fine, but when I tried my own test case, I had a problem. Apparently the OriginC compiler does not like "void" return types for function pointers. If I uncomment the #define as instructed below in my example, the Origin session crashes.

Dave

//------------------------------------------------------------
#include <origin.h>

// Uncomment to force a crash of OriginC in OriginPro 7SR2 (v7.0383)
// on WinXP
//#define CRASHME

#ifdef CRASHME
#define RETURNTYPE void
#else
#define RETURNTYPE int
#endif

typedef RETURNTYPE (*MYFUNCP)(int *ip);

RETURNTYPE printx(int *ip)
{
printf("inside printx\n");
return 0;
}

void call_func(MYFUNCP fp)
{
int i = 0;
fp(&i);
return;
}

void test_funcp(void)
{
call_func(printx);
return;
}
//-------------------------------------------------------------------

dlw21 Posted - 12/09/2002 : 12:16:16 PM
Easwar,

This is very interesting. I have 7SR2, but was unaware that this feature had been added.

I was able to get your example to compile and run just fine, but when I tried my own test case, I had a problem. Apparently the OriginC compiler does not like "void" return types for function pointers. If I uncomment the #define as instructed below in my example, the Origin session crashes.

Dave

//------------------------------------------------------------
#include <origin.h>

// Uncomment to force a crash of OriginC in OriginPro 7SR2 (v7.0383)
// on WinXP
//#define CRASHME

#ifdef CRASHME
#define RETURNTYPE void
#else
#define RETURNTYPE int
#endif

typedef RETURNTYPE (*MYFUNCP)(int *ip);

RETURNTYPE printx(int *ip)
{
printf("inside printx\n");
return 0;
}

void call_func(MYFUNCP fp)
{
int i = 0;
fp(&i);
return;
}

void test_funcp(void)
{
call_func(printx);
return;
}
//-------------------------------------------------------------------

easwar Posted - 12/06/2002 : 1:57:49 PM
Hi Dave,

Support for function pointers was added in 7SR2. You can get the SR2 patch from our downloads area.

Pasted here is a sample code for quick sort that uses function pointers.

Easwar
OriginLab.


//--------------------------------------------------------------------------------------------------------------------
#define size_t unsigned int
static void MySwap( char *a, char *b, size_t width )
{
char tmp;

if ( a != b )
// Do the swap one character at a time to avoid potential alignment
// problems.
while ( width-- ) {
tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}


static int DoubleComp( const void *arg1, const void *arg2 )
{
double *ldLeft, *ldRight;
ldLeft = (double *)arg1;
ldRight = (double *)arg2;
if ( *ldLeft > *ldRight )
return 1;
else if( *ldLeft < *ldRight )
return -1;
return 0;
}

typedef int (*MYFUNPTRTYPE )(const void *elem1, const void *elem2 );



void MyQsort( void *base, size_t num, size_t width, MYFUNPTRTYPE MyComp )
{
LPSTR lpLeft, lpRight, lpMid, lpLast, lpTemp;

out_str("Entering MyQsort...");


if (num < 2 || width == 0)
{
out_str("EXITING MyQsort...");

return; // nothing to do
}

lpLeft = (char *)base;
lpRight = (char *)base + ( num - 1 )*width;
lpMid = (char *)base + num/2*width;
MySwap( lpLeft, lpMid, width);
lpLast = lpLeft;
for ( lpTemp = lpLeft + width; lpTemp <= lpRight; lpTemp += width )
{
if ( MyComp( lpTemp, lpLeft ) < 0 ) // runtime error here
//if ( DoubleComp( lpTemp, lpLeft ) < 0 ) // runtime error here
{
lpLast += width;
MySwap( lpLast, lpTemp, width );

}
}
MySwap( lpLeft, lpLast, width );
MyQsort(lpLeft, ( lpLast - lpLeft )/ width, width, MyComp);
MyQsort(lpLast + width, ( lpRight - lpLast )/ width, width, MyComp);

out_str("EXITING MyQsort...");
}

void test11()
{

double dd[7] = { 1, 7, 11, 4, 5, 3, 6};

MyQsort(dd, 7, sizeof(double), DoubleComp);

for( int i = 0; i < 7; i++ )
printf (" %f, ", dd[i]);
}
//-----------------------------------------------------------------------------------------------------------------------------



Edited by - easwar on 12/06/2002 2:03:46 PM
dlw21 Posted - 12/06/2002 : 08:31:13 AM
Hello,

While the above typedef appears to compile a-ok, I can't get code that *uses* it to compile.

Unfortunately, the Origin Programming Guide states pretty unambiguously in the section 'Origin C Programmer's Guide:The Origin Language:Supported Basic and Derived Types':

"=> Origin C does not support function pointers"

I hope OriginLab has plans to add this functionality in the future!

Dave

ML Posted - 12/05/2002 : 5:01:24 PM
Try to typedef the function pointer like this:

typedef void (*MYFUNCTYPE)(float, float, float [], float *, float [], int);

and use it like this:

void mrqcof([..other args..], MYFUNCTYPE funcs){
[..]
funcs(x[i],dfx[dfi],a,&ymod,dyda,ma);
[..]
}


The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000