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
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 passing a *function to another function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

lilo

Germany
2 Posts

Posted - 12/05/2002 :  09:37:31 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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

ML

USA
63 Posts

Posted - 12/05/2002 :  5:01:24 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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);
[..]
}

Go to Top of Page

dlw21

USA
18 Posts

Posted - 12/06/2002 :  08:31:13 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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

Go to Top of Page

easwar

USA
1965 Posts

Posted - 12/06/2002 :  1:57:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

dlw21

USA
18 Posts

Posted - 12/09/2002 :  12:16:16 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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;
}
//-------------------------------------------------------------------

Go to Top of Page

dlw21

USA
18 Posts

Posted - 12/09/2002 :  12:16:19 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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;
}
//-------------------------------------------------------------------

Go to Top of Page

Barb Tobias

USA
305 Posts

Posted - 12/09/2002 :  1:49:26 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Dave,
There were quite a few Origin C improvements in SR2. Check out the release notes.
-Barb

Go to Top of Page

easwar

USA
1965 Posts

Posted - 12/09/2002 :  2:24:48 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Dave,

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

Easwar
OriginLab.
Go to Top of Page

ML

USA
63 Posts

Posted - 12/09/2002 :  6:05:48 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The void return crash issue will be taken care of in the upcoming service release.
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000