Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to function question

Reply
Thread Tools

pointer to function question

 
 
mdh
Guest
Posts: n/a
 
      05-15-2007
I am curious as to why the lines commented out also seem to work?( I
thought that the declaration of the pointer needed to mimic the
function it is supposed to point to, hence I expected void(*p)(char)
to work, which it does.)

#include <stdio.h>

int main (int argc, const char * argv[]) {
void myf(char);
void (*p)(char);
/* also works: void (*p)(); */
/* also works: int (*p)(); */

p=myf;
p('u');
return 0;
}


void myf( char c){
printf("Character is %c\n", c);
}

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-15-2007
mdh wrote:
> I am curious as to why the lines commented out also seem to work?( I
> thought that the declaration of the pointer needed to mimic the
> function it is supposed to point to, hence I expected void(*p)(char)
> to work, which it does.)
>

Pot luck, the correct value happens to end up the the right place. Did
you note the warnings your compiler gave you?

> #include <stdio.h>
>
> int main (int argc, const char * argv[]) {
> void myf(char);
> void (*p)(char);
> /* also works: void (*p)(); */
> /* also works: int (*p)(); */
>
> p=myf;
> p('u');
> return 0;
> }
>
>
> void myf( char c){
> printf("Character is %c\n", c);
> }
>



--
Ian Collins.
 
Reply With Quote
 
 
 
 
mdh
Guest
Posts: n/a
 
      05-15-2007

> mdh wrote:
> > I am curious as to why the lines commented out also seem to work?..........


> > #include <stdio.h>

>
> > int main (int argc, const char * argv[]) {
> > void myf(char);
> > void (*p)(char);
> > /* also works: void (*p)(); */
> > /* also works: int (*p)(); */

>
> > p=myf;
> > p('u');
> > return 0;
> > }

>
> > void myf( char c){
> > printf("Character is %c\n", c);
> > }




Ian Collins <(E-Mail Removed)> wrote:
> Pot luck, the correct value happens to end up the the right place. Did
> you note the warnings your compiler gave you?



warning..."assignment from incompatible pointer type".

So, the fact that it gives the "correct" result is just plain luck?






 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-15-2007
mdh <(E-Mail Removed)> writes:
>> mdh wrote:
>> > I am curious as to why the lines commented out also seem to
>> > work?..........

>
>> > #include <stdio.h>

>>
>> > int main (int argc, const char * argv[]) {
>> > void myf(char);
>> > void (*p)(char);
>> > /* also works: void (*p)(); */
>> > /* also works: int (*p)(); */

>>
>> > p=myf;
>> > p('u');
>> > return 0;
>> > }

>>
>> > void myf( char c){
>> > printf("Character is %c\n", c);
>> > }

>
>
>
> Ian Collins <(E-Mail Removed)> wrote:
>> Pot luck, the correct value happens to end up the the right place. Did
>> you note the warnings your compiler gave you?

>
>
> warning..."assignment from incompatible pointer type".
>
> So, the fact that it gives the "correct" result is just plain luck?


Yes, *bad* luck. (If you had *good* luck, your implementation would
catch the error earlier.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-15-2007
mdh said:

> I am curious as to why the lines commented out also seem to work?


Experimentation is often a very useful tool in programming, but in
learning a programming language it is a definite handicap.

If you're writing a novel in English, you might well try out something
like "Pete, a pipe, a Pict, a peck of pickled pepper" just to see
whether it pans out as you wanted it to, and that's legitimate.

But if you are *learning* English, it isn't good enough to string words
together almost at random and wonder whether they work. They mite eve
an sow and rite too ewe, but communicating with another English-speaker
in that way is fraught with peril. So it is with the compiler.
Especially if you silence its attempts to warn you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      05-15-2007
On May 14, 10:24 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> mdh said:
>
> > I am curious as to why the lines commented out also seem to work?

>
> Experimentation is often a very useful tool in programming, but in
> learning a programming language it is a definite handicap.
>
>



ok....point taken. Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments