Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Reusing function signature for both a function pointer and function. (http://www.velocityreviews.com/forums/t954491-reusing-function-signature-for-both-a-function-pointer-and-function.html)

daniel rich 11-13-2012 11:15 PM

Reusing function signature for both a function pointer and function.
 
Hello All,

I have been wrestling with a problem, and my others searches have failed so I wanted to ask here.

Thanks in advance for any thoughts/help.

I currently have some code that looks something like this.

someHeader.h
"
typedef void (* proj_getNameFuncPtr) (char* nameBuffer );

void proj_getName( char* nameBuffer );
"
someFile.c
"
void proj_getName(char* nameBuffer )
{
//implement
}
"


Ideally I was hoping to do something like the following instead, so that I can reduce the code duplication. In the actual project there are quite a few of these and
maintaining the signature in both the typedef'd func pointer and the function dec'l is a pain.

newHeader.h
"
typedef void proj_getNameFunc( char* nameBuffer );

typedef proj_getNameFunc* proj_getNameFuncPtr;

proj_getNameFunc proj_getName;
"
newFile.c
"
void proj_getName(char* nameBuffer )
{
//implement
}
"

The compiler I am using(msvc71) chokes on "typedef proj_getNameFunc* proj_getNameFuncPtr;" and complains.
"error: expected '=', ',', ';', 'asm' or '__attribute__' before * token"


I want to keep the func pointers around since we do end up using quite a few of them in function signatures. I may be doing things the totally wrong way but would appreciate any correction/help that could be provided.

--Daniel Rich

Ian Collins 11-13-2012 11:23 PM

Re: Reusing function signature for both a function pointer and function.
 
On 11/14/12 12:15, daniel rich wrote:

** please wrap your lines! *

> Hello All,
>
> I have been wrestling with a problem, and my others searches have
> failed so I wanted to ask here.
>
> Thanks in advance for any thoughts/help.
>
> I currently have some code that looks something like this.
>
> someHeader.h " typedef void (* proj_getNameFuncPtr) (char* nameBuffer
> );
>
> void proj_getName( char* nameBuffer ); " someFile.c " void
> proj_getName(char* nameBuffer ) { //implement } "
>
>
> Ideally I was hoping to do something like the following instead, so
> that I can reduce the code duplication. In the actual project there
> are quite a few of these and maintaining the signature in both the
> typedef'd func pointer and the function dec'l is a pain.


Firstly, why do you have to maintain both?

What do you use the function pointer type for and where is the duplication?

> newHeader.h " typedef void proj_getNameFunc( char* nameBuffer );
>
> typedef proj_getNameFunc* proj_getNameFuncPtr;
>
> proj_getNameFunc proj_getName; " newFile.c " void proj_getName(char*
> nameBuffer ) { //implement } "
>
> The compiler I am using(msvc71) chokes on "typedef proj_getNameFunc*
> proj_getNameFuncPtr;" and complains. "error: expected '=', ',', ';',
> 'asm' or '__attribute__' before * token"


If the error is caused by the code you posted, the compiler is broken...

> I want to keep the func pointers around since we do end up using
> quite a few of them in function signatures. I may be doing things the
> totally wrong way but would appreciate any correction/help that could
> be provided.


How? Some use cases might make your problem clearer.

--
Ian Collins

Barry Schwarz 11-14-2012 02:20 AM

Re: Reusing function signature for both a function pointer and function.
 
On Tue, 13 Nov 2012 15:15:34 -0800 (PST), daniel rich
<qianyilong@gmail.com> wrote:

snip

>Ideally I was hoping to do something like the following instead, so that I can reduce the code duplication. In the actual project there are quite a few of these and
>maintaining the signature in both the typedef'd func pointer and the function dec'l is a pain.
>
>newHeader.h
>"
>typedef void proj_getNameFunc( char* nameBuffer );
>
>typedef proj_getNameFunc* proj_getNameFuncPtr;
>
>proj_getNameFunc proj_getName;
>"
>newFile.c
>"
>void proj_getName(char* nameBuffer )
>{
>//implement
>}
>"
>
>The compiler I am using(msvc71) chokes on "typedef proj_getNameFunc* proj_getNameFuncPtr;" and complains.
>"error: expected '=', ',', ';', 'asm' or '__attribute__' before * token"
>
>
>I want to keep the func pointers around since we do end up using quite a few of them in function signatures. I may be doing things the totally wrong way but would appreciate any correction/help that could be provided.


My compiler which is at the C90 level accepts the two typedefs and the
prototype. Are you sure what you posted is exactly what you compiled?

Once you get it to work, you still have the problem of "synching" the
parameter list in the function definition with the one in the typedef.
Have you considered using a #define.

#define proj_getNameFuncParm type1 name1, type2 name 2
typedef void proj_getNameFunc( proj_getNameFuncParm);
typedef proj_getNameFunc* proj_getNameFuncPtr;
proj_getNameFunc proj_getName;

void proj_getName(proj_getNameFuncParm)
{
//implement
}

--
Remove del for email

Heikki Kallasjoki 11-14-2012 10:07 AM

Re: Reusing function signature for both a function pointer andfunction.
 
On 2012-11-14, Robert Wessel <robertwessel2@yahoo.com> wrote:
> But *what* compiler are you using? MSVC 7.1 is 20 years old! The
> oldest version I could easily run (v8.0 - circa 1993) seems just fine.


20 years? Circa 1993?

"Visual C++ .NET 2003 (known also as Visual C++ 7.1), which included MFC
7.1, was released in 2003 --"

"Visual C++ 2005 (known also as Visual C++ 8.0), which included MFC 8.0,
was released in November 2005 --"

These are the compilers that I believe the names "MSVC 7.1" and "MSVC
8.0" are most often used to refer to. Versions before the "Visual C++"
brand ("Microsoft C/C++"), as far as I know, don't go beyond 7.0, since
Visual C++ 1.0 was the follow-up to that. Disclaimer: most of the above
is second-hand knowledge; though I did use "MSVC 6" aka "Visual C++ 6.0"
quite a lot, and it was the new thing in 1998.

(Admittedly, it will soon be a decade since 2003...)

--
Heikki Kallasjoki

Noob 11-14-2012 10:25 AM

Re: Reusing function signature for both a function pointer and function.
 
Robert Wessel wrote:

> MSVC 7.1 is 20 years old! The oldest version I could easily run
> (v8.0 - circa 1993) seems just fine.


??

https://en.wikipedia.org/wiki/Visual...istory_.5B2.5D


daniel rich 11-14-2012 04:03 PM

Re: Reusing function signature for both a function pointer and function.
 
I am mortified to admit that after all of the comments that it should
work I realized that I did in fact have a typo in the original code.
It now works as intended and I may also use the #define for arguments as
suggested above.

Sorry for the wasted time and thanks for the suggestions.

The compiler is somewhat old but mandated for the project, and the funcPtr
typedef are needed b/c they are used through the codebase, and I only
have control over a small portion of said codebase.


All times are GMT. The time now is 02:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.