Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to cast function pointer? (http://www.velocityreviews.com/forums/t315477-how-to-cast-function-pointer.html)

Devrobcom 09-26-2003 12:03 PM

How to cast function pointer?
 
Hi
I know that this is not the Lint forum, but I get some Lint warnings
when setting my pointers to NULL. And the problem is
I don't know howto cast a function ptr the right way.

typedef int FP(char*, char*);

FP *pfp;
pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to
void/nonvoid))

I have tried, without success:
pft = (FP*)NULL;
pfp = (int(*)(char*, char*))NULL;




Richard Bos 09-26-2003 12:18 PM

Re: How to cast function pointer?
 
"Devrobcom" <Devrobcom@hotmail.com> wrote:

> I know that this is not the Lint forum, but I get some Lint warnings
> when setting my pointers to NULL. And the problem is
> I don't know howto cast a function ptr the right way.
>
> typedef int FP(char*, char*);
>
> FP *pfp;
> pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to
> void/nonvoid))


That is an incorrect error. NULL is required to be a null pointer
constant; therefore, when being assigned to _any_ type of pointer, it is
required to result in a null pointer of the appropriate type; and null
pointers can, without problem, be assigned to any kind of pointer, no
matter whether they're object or function pointers. Lint is talking out
of its hat. There is no more correct way to do this - what you've done
is already quite correct.

Richard

Derk Gwen 09-26-2003 12:48 PM

Re: How to cast function pointer?
 
"Devrobcom" <Devrobcom@hotmail.com> wrote:
# Hi
# I know that this is not the Lint forum, but I get some Lint warnings
# when setting my pointers to NULL. And the problem is
# I don't know howto cast a function ptr the right way.
#
# typedef int FP(char*, char*);
#
# FP *pfp;
# pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to
# void/nonvoid))
#
# I have tried, without success:
# pft = (FP*)NULL;
# pfp = (int(*)(char*, char*))NULL;

I don't use NULL, I just use 0.

cat <<':eof' >/tmp/t.c
void f(void) {
typedef int FP(char*, char*);
FP *pfp;
pfp = 0;
}
:eof
cc -c /tmp/t.c


--
Derk Gwen http://derkgwen.250free.com/html/index.html
I hope it feels so good to be right. There's nothing more
exhilirating pointing out the shortcomings of others, is there?


All times are GMT. The time now is 10:24 AM.

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