473,320 Members | 1,896 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,320 software developers and data experts.

How to declare pointer to functions in structures?

Hi!

I'm trying to declare a structure that will be including pointers to
functions. These structures in turn will be pointed to in an array and
depending on a index value that specific function in the structure pointed
to by the index will be called with various data.

Say i have a couple of functions:

int func1(int , int);
int func2(int, int);

Then i have a structure:

typedef struct {
int someval;
/* Below is where i'm not sure how to declare,
* here is where one of the functions above will be inserted */
int *somefunction();
} mystruct;

Then i initialize the array wich should point to structures:

mystruct myarray[] = {{10,func1()},{20,func2()},{30,func1()}};

Now when executing the line below and say array_index below is 0 it will
call function func1():

myarray[array_index]->somefunction(value1, value2);

Anybody understand what i mean? I just don't know how to declare the
function in the structure "mystruct" above.

Mats
Jul 23 '05 #1
2 3463
Mackan wrote:
I'm trying to declare a structure that will be including pointers to
functions. These structures in turn will be pointed to in an array and
depending on a index value that specific function in the structure pointed
to by the index will be called with various data.

Say i have a couple of functions:

int func1(int , int);
int func2(int, int);

Then i have a structure:

typedef struct {
int someval;
/* Below is where i'm not sure how to declare,
* here is where one of the functions above will be inserted */
int *somefunction();
Should be

int (*somefunction)(int,int);
} mystruct;

Then i initialize the array wich should point to structures:

mystruct myarray[] = {{10,func1()},{20,func2()},{30,func1()}};

Now when executing the line below and say array_index below is 0 it will
call function func1():

myarray[array_index]->somefunction(value1, value2);

Anybody understand what i mean? I just don't know how to declare the
function in the structure "mystruct" above.

V
Jul 23 '05 #2
Your declaration of 'somefunction' in 'mystruct' reads 'somefunction'
is a function taking no arguments and returning type pointer to int.
Because of operator precedence in C++ you'll need parentheses so the
declaration should be

int (*somefunction)();

which reads 'somefunction' is a pointer to a function taking no
arguments and returning type int, this declaration is very different
than what you were doing initially and is probably what you want.

Just something to keep in mind, have you considered using pure abstract
classes instead of pointers to functions? Often times (not always) they
lead to a more elegant design.

- pete

Jul 23 '05 #3

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

Similar topics

16
by: fix | last post by:
Hi all, I am new to C and I just started to program for a homework. I included my code down there. It is a fraction "class". I declared the Fraction struct, tried to create some invalid fraction,...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
11
by: quakewang | last post by:
hi, I have define in a head file like this: #define GLUT_BITMAP_9_BY_15 ((void*)2) #define GLUT_BITMAP_8_BY_13 ((void*)3) #define GLUT_BITMAP_TIMES_ROMAN_10 ...
2
by: guy.gorodish | last post by:
hi, i have couple of qeustions: 1. I have built a interop of c dll. In this dll i have a function that expect a pointer to a struct. I want to pass a struct from my c# interop into this c...
6
by: etienne | last post by:
Hello, I am looking for help for a cast problem: I want to run functions in thread, using the pthread_create function. The function to run is the first argument of pthread_create and is expected...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
2
by: =?Utf-8?B?dmxhZGltaXI=?= | last post by:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll functions do: 1) allocate global memory for internal structures 2) controls dll subsystem (communicate by sockets,...
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.