Reusing function signature for both a function pointer and function.

D

daniel rich

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
 
I

Ian Collins

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.
 
B

Barry Schwarz

On Tue, 13 Nov 2012 15:15:34 -0800 (PST), daniel rich

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
}
 
H

Heikki Kallasjoki

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...)
 
D

daniel rich

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top