473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

pointer to function question

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

May 15 '07 #1
5 1319
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.
May 15 '07 #2
mdh
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 <ian-n...@hotmail.comwrote:
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?


May 15 '07 #3
mdh <md**@comcast.netwrites:
>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 <ian-n...@hotmail.comwrote:
>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) ks***@mib.org <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"
May 15 '07 #4
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.
May 15 '07 #5
mdh
On May 14, 10:24 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.

May 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: Marcus Jacobs | last post by:
Before I am flamed, I did search the FAQ for an answer to the question that I am about to post. Even with a search of the newsgroup archive, there are so many subjects about pointers I could search...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.