473,804 Members | 4,153 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
11 1707
On Thu, 27 Jul 2006 06:13:14 UTC, "Michael" <mi*********@ya hoo.com>
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*);
No, you means

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

as the 3th parameter is truly not a pointer to void but to a function
returning voind and getting a single parameter of type pointer to
void. You have to declare the prototype always like a function
definition - but without the definition itself.

void getInt(int*);
Better selfdocumenting :
void getInt(int *ptr);
void showInt(int*);
and
void showInt(int *ptr);

Ther compiler will still ignore the name of the parameter in a
prototype but the reader will assign some description with it when the
name of the parameter does describe it.
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


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jul 28 '06 #11
On 28 Jul 2006 16:42:43 GMT, mw*****@newsguy .com (Michael Wojcik)
wrote:
>
In article <11************ *********@m79g2 000cwm.googlegr oups.com>, "lovecreatesbea uty" <lo************ ***@gmail.comwr ites:
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. <snip>
Also "true" array pointers, that is with actual type pointer to array
instead of the normal convention of pointer to (first) element.
Although it is arguable these are less common -- and certainly less
vital -- than function pointers.

The syntax also doesn't work for function types and array types --
int (double) * and int [5] * are syntactically invalid -- but as those
types can never be arguments (var or not) anyway, the inability to
fetch them with va_arg becomes of distinctly lesser importance.

- David.Thompson1 at worldnet.att.ne t
Aug 7 '06 #12

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

Similar topics

1
6739
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
3414
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
1518
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
2874
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
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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
10323
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
9161
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...
0
6856
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
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
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.