pointer to function question

M

mdh

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);
}
 
I

Ian Collins

mdh said:
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);
}
 
M

mdh

mdh said:
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 said:
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?
 
K

Keith Thompson

mdh said:
mdh said:
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 said:
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.)
 
R

Richard Heathfield

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

mdh

mdh said:


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


ok....point taken. Thanks.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top