473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

easy pointers to functions problem

Hi,

I am trying to get an idea of how function pointers work.
I have the following:

#include <stdio.h>

void do_stuff(int*,i nt,void*);
void getInt(int*);
void showInt(int*);

int main(){

int num_array[10];

do_stuff(num_ar ray, 10, getInt);
do_stuff(num_ar ray, 10, showInt);

return(0);
}

void do_stuff(int *a, int size, void (*process)(int *)){
int i;
for(i = 0; i < size; i++){
process(&a[i]);
}
}

void getInt(int *ptr){
if((scanf("%d", ptr)) != 1){
printf("error") ;

}
}

void showInt(int *ptr){
printf("%d", *ptr);
}

but I get errors "conficting types for do_stuff" and also warnings "ISO C
forbids passing of arg3 of do_stuff between pointer and void *"

I'm getting nowhere fast here. Can someone help me out with what I have
wrong?

Thanks

Michael
Jul 27 '06 #1
11 1705

Michael wrote:
#include <stdio.h>

void do_stuff(int*,i nt,void*);
void do_stuff(int *a, int size, void (*process)(int *));

<snip>
void do_stuff(int *a, int size, void (*process)(int *)){
int i;
<snip>

Jul 27 '06 #2
Michael wrote:
Hi,

I am trying to get an idea of how function pointers work.
I have the following:

#include <stdio.h>

void do_stuff(int*,i nt,void*);
Here the last parameter of do_stuff is void*. void* is *not* compatible
with function pointers, only object pointers (i.e. pointers to data).
I.e. you cannot pass a function pointer as a void* parameter.
void getInt(int*);
void showInt(int*);

int main(){
If you are not using the parameters, it is better to be explicit.

int main(void) {
int num_array[10];

do_stuff(num_ar ray, 10, getInt);
do_stuff(num_ar ray, 10, showInt);
Why the magic number 10? I would use a #define for the array size, then
there is only one place to change it.
return(0);
}

void do_stuff(int *a, int size, void (*process)(int *)){
This is different to your prototype of do_stuff above, just as the
compiler told you! It is this that is correct, so change your earlier
prototype to match.
int i;
for(i = 0; i < size; i++){
process(&a[i]);
}
}

void getInt(int *ptr){
if((scanf("%d", ptr)) != 1){
printf("error") ;
You might want a /n at the end of that printf. Or use puts.
}
}

void showInt(int *ptr){
printf("%d", *ptr);
}

but I get errors "conficting types for do_stuff"
The above error is obvious. Your prototype and definition don't match.
It's a bit like you telling someone you will give them an orange and
then giving them a cricket bat, of course they will tell you that they
are different!
and also warnings "ISO C
forbids passing of arg3 of do_stuff between pointer and void *"
This is less obvious. void* is only a generic pointer to object (i.e.
data), it cannot point to functions. Imagine a system with 32bit data
pointers and 64bit function pointers. 64 does not do in to 32!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 27 '06 #3

Flash Gordon wrote:
Michael wrote:
printf("error") ;

You might want a /n at the end of that printf. Or use puts.
Do you point to a '\n'.

Jul 27 '06 #4
lovecreatesbeau ty wrote:
Flash Gordon wrote:
>Michael wrote:
>> printf("error") ;
You might want a /n at the end of that printf. Or use puts.

Do you point to a '\n'.
Indeed. Thank's for the correction.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 27 '06 #5

"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
news:t2******** ****@news.flash-gordon.me.uk...
Michael wrote:
>Hi,

I am trying to get an idea of how function pointers work.
I have the following:

#include <stdio.h>

void do_stuff(int*,i nt,void*);

Here the last parameter of do_stuff is void*. void* is *not* compatible
with function pointers, only object pointers (i.e. pointers to data). I.e.
you cannot pass a function pointer as a void* parameter.
I thought this was where my problem was, but when I tried to declare it many
other ways
I always ended up with a syntax error.

Could you tell me how I should declare it please? In the book I am working
from none of the
function prototypes are declared, they are all just declared implicitly.....
>
>void getInt(int*);
void showInt(int*);

int main(){

If you are not using the parameters, it is better to be explicit.

int main(void) {
> int num_array[10];

do_stuff(num_ar ray, 10, getInt);
do_stuff(num_ar ray, 10, showInt);

Why the magic number 10? I would use a #define for the array size, then
there is only one place to change it.
I was aware that I had a 'magic' number here, but as I was only writing this
to try and figure out
where everything else was wrong, I didn't bother with the #define. My
appologies.
>
> return(0);
}

void do_stuff(int *a, int size, void (*process)(int *)){

This is different to your prototype of do_stuff above, just as the
compiler told you! It is this that is correct, so change your earlier
prototype to match.
> int i;
for(i = 0; i < size; i++){
process(&a[i]);
}
}

void getInt(int *ptr){
if((scanf("%d", ptr)) != 1){
printf("error") ;

You might want a /n at the end of that printf. Or use puts.
again, I don't really care about the output, I just want to get the function
pointer stuff working, but fair comment.

Thanks for the help, I really apppreciate it.
> }
}

void showInt(int *ptr){
printf("%d", *ptr);
}

but I get errors "conficting types for do_stuff"

The above error is obvious. Your prototype and definition don't match.
It's a bit like you telling someone you will give them an orange and then
giving them a cricket bat, of course they will tell you that they are
different!
and also warnings "ISO C
forbids passing of arg3 of do_stuff between pointer and void *"

This is less obvious. void* is only a generic pointer to object (i.e.
data), it cannot point to functions. Imagine a system with 32bit data
pointers and 64bit function pointers. 64 does not do in to 32!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Jul 27 '06 #6
Michael wrote:
"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
news:t2******** ****@news.flash-gordon.me.uk...
>Michael wrote:
>>Hi,

I am trying to get an idea of how function pointers work.
I have the following:

#include <stdio.h>

void do_stuff(int*,i nt,void*);
Here the last parameter of do_stuff is void*. void* is *not* compatible
with function pointers, only object pointers (i.e. pointers to data). I.e.
you cannot pass a function pointer as a void* parameter.

I thought this was where my problem was, but when I tried to declare it many
other ways
I always ended up with a syntax error.

Could you tell me how I should declare it please? In the book I am working
from none of the
function prototypes are declared, they are all just declared implicitly.....
Further down you defined the function as
void do_stuff(int *a, int size, void (*process)(int *)){
/* stuff */
}

Therefore, a prototype for it would be
void do_stuff(int *a, int size, void (*process)(int *));
I.e. the only difference is it ends with a semi-colon instead of being
followed by the body of the function.

It is good that you have decided to not rely on implicit declarations,
since they do not always work properly.

<snip>
again, I don't really care about the output, I just want to get the function
pointer stuff working, but fair comment.

Thanks for the help, I really apppreciate it.
No problem. I point out more than your current problem (as do others)
because this gives you more opportunity to learn.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 27 '06 #7
Michael wrote:
Hi,

I am trying to get an idea of how function pointers work.
I have the following:
but I get errors "conficting types for do_stuff" and also warnings
"ISO C forbids passing of arg3 of do_stuff between pointer and void *"

I'm getting nowhere fast here. Can someone help me out with what I
have wrong?
This is one of those cases where typedefs can be useful:

<http://c-faq.com/decl/pfitypedef.html >


Brian
Jul 27 '06 #8

Default User wrote:
This is one of those cases where typedefs can be useful:

<http://c-faq.com/decl/pfitypedef.html >
I think typedef is unuseful totally.

Jul 28 '06 #9

In article <11************ *********@m79g2 000cwm.googlegr oups.com>, "lovecreatesbea uty" <lo************ ***@gmail.comwr ites:
>
Default User wrote:
This is one of those cases where typedefs can be useful:

<http://c-faq.com/decl/pfitypedef.html >

I think typedef is unuseful totally.
There is at least situation where it is the only solution: for
using certain types with the va_arg macro. va_arg requires that
if used with type T, as in va_arg(ap, T), that T* be a pointer
to an object of type T.

This doesn't work with function pointers. Given a function
pointer of type "T (*)(P)", a pointer-to-function-pointer for that
type is "T (**)(P)". "T (*)(P)*" is syntactically invalid.

So if you have a variadic function that takes arguments with
function-pointer types, you MUST use typedef in order to retrieve
those arguments. typedef works because if you create a typedef
TD for a function pointer type, then TD* is a pointer-to-function-
pointer.

Now you could wrap those function pointers in structs and pass
them that way, but that's inconvenient for the caller, and may not
be the interface you want.

--
Michael Wojcik mi************@ microfocus.com

I am a realistic background detail. -- Bruce Schneier (on his mention in
_The Da Vinci Code_)
Jul 28 '06 #10

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

Similar topics

1
6738
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double ); void SomeClass::DoSomething( unsigned int num_something, unsigned long num_something_else, ptr2FuncType1 handler_one, ... ) The functions I am passing in are in a different cpp file, but they
2
2013
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions that print out a type's name to an istream:
3
1684
by: Filipe Valepereiro | last post by:
I all. I need to write a function that convert one string into a vector. This string represent a serialized form of the vector. So i come up with this piece of code, that compile just fine. The problem is linking, VC6++ give me this error: unresolved external symbol "class std::vector<double,class std::allocator<double> > __cdecl VectorFromStr(class EFAString
3
425
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback functions. Important is, that the object A.o has to be created BEFORE B can be compiled. I am sure that sounds confusing but A.cpp is part of a library that is needed for objects I use in B (kind of client-server thing). So I cannot include a B.h in the...
27
3413
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
9
5068
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar = getter(file); What type should I give to `getter' so that the compiler does not issue
2
3285
by: David Williams | last post by:
Hi Guys, I have a couple of problems. The first is (I believe) a simple syntactic problem you can probably solve quite easily. The second is a design style problem which might be more tricky... So, I have a class with a couple of integers. I provide a couple of functions to operate on those integers (add, subtract) and the function to be called is chosen via a function pointer. Like so: 01: #include <iostream> 02:
7
1517
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
12
2873
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair of pointers, where each is a pointer to the base class (each is a SPRITE *). I know that each of these pointers actually points to one of the derived classes, even though the type of the pointer is SPRITE *, but I don't know which derived class it...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.