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

Access from function in struct (pointer to function) to other items in the struct (advanced)

Ole
hello,

Little problem:

struct operatable {
char * operatable_id;
int ( * delegate ) ( ... );
somedatatype data;
};

When i now have designed a function to int (*delegate)() and i call it

struct operatable * t = ( struct operatable * ) malloc (
sizeof( struct operatable )
);

t->delegate( someargumenttype arg );

How then is it possible to do something like this:

int delegate_impl ( someargumenttype arg ) {
// do something with arg, parse etc.
printf ( "Called delegate() from within : %s\n",
<reference_on_containingstruct>->operatable_id
);
}
Nov 14 '05 #1
4 2085
"Ole" <ov*@gmx.com> wrote in message
news:4b**************************@posting.google.c om...
hello,

Little problem:

struct operatable {
char * operatable_id;
int ( * delegate ) ( ... );
somedatatype data;
};

When i now have designed a function to int (*delegate)() and i call it

struct operatable * t = ( struct operatable * ) malloc (
sizeof( struct operatable )
);

t->delegate( someargumenttype arg );

How then is it possible to do something like this:

int delegate_impl ( someargumenttype arg ) {
// do something with arg, parse etc.
printf ( "Called delegate() from within : %s\n",
<reference_on_containingstruct>->operatable_id
);
}


You would have to pass the struct pointer as
part of the function parameter list (most likely
the first parameter, because it's always required).
Nov 14 '05 #2

In article <4b**************************@posting.google.com >, ov*@gmx.com (Ole) writes:

struct operatable {
char * operatable_id;
int ( * delegate ) ( ... );
somedatatype data;
};
I assume "..." above is actually replaced with a real parameter
list. C variadic function notation requires at least one non-
variadic parameter. A function pointer type with an argument list
of "(...)" is illegal.
When i now have designed a function to int (*delegate)() and i call it
You mean you've assigned a value to "delegate"?
struct operatable * t = ( struct operatable * ) malloc (
sizeof( struct operatable )
);
I hope this comes *before* you assign anything to "delegate". Also,
note that you should not cast the return value of malloc, and it's
safer to apply the sizeof operator to the dereferenced pointer than
to its type, since the latter may change in a later version of the
program:

#include <stdlib.h>
struct opertable *t = malloc(sizeof *t);

And then check for malloc failure.
t->delegate( someargumenttype arg );
OK, if the definition of struct opertable actually has something like

int (*delegate)(someargumenttype);
How then is it possible to do something like this:

int delegate_impl ( someargumenttype arg ) {
// do something with arg, parse etc.
printf ( "Called delegate() from within : %s\n",
<reference_on_containingstruct>->operatable_id
);
}


This is not entirely clear. If you intend to use delegate_impl as
a possible value for your delegate field, then it will not have
access to the struct opertable * variable through which it was
called. If you want such access, you'll have to pass it. For
example:

struct operatable
{
char *operatable_id;
int (*delegate)(struct opertable *, somedatatype);
somedatatype data;
};

int delegate_impl(struct opertable *caller, somedatatype arg)
{
/* ... */
printf("Called delegate from %s\n", caller->opertable_id;
return 0;
}

By the way, putting whitespace between the "*" and the identifier
name when specifying pointer types is a bad idea. It can lead to
errors like:

char * pointer1, pointer2;

where "pointer2" is in fact a char, not a char *, despite its name.

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

Do we have boyfriends? We are interested in delicious food and sweets.
And tiny animals like the cat. -- Naoko Yamano
Nov 14 '05 #3
Ole
mw*****@newsguy.com (Michael Wojcik) wrote in message news:<cl*********@news3.newsguy.com>...
In article <4b**************************@posting.google.com >, ov*@gmx.com (Ole) writes:

struct operatable {
char * operatable_id;
int ( * delegate ) ( ... );
somedatatype data;
};
I assume "..." above is actually replaced with a real parameter
list. C variadic function notation requires at least one non-
variadic parameter. A function pointer type with an argument list
of "(...)" is illegal.


Yes. I know that the variable argument list can only be defined, when
at least
one memory address of a given argument is available. With (...) i
meant, that the arguments are not essential for this problem.
When i now have designed a function to int (*delegate)() and i call it


You mean you've assigned a value to "delegate"?

Yes. I meant to pass a function pointer to the "delegate" variable.

By the way, is

int ( * delegate ) ( int x, ... );

still a variable? I would say it is, since pointers are only variables
that
can hold memory a address.
struct operatable * t = ( struct operatable * ) malloc (
sizeof( struct operatable )
);


I hope this comes *before* you assign anything to "delegate". Also,
note that you should not cast the return value of malloc, and it's
safer to apply the sizeof operator to the dereferenced pointer than
to its type, since the latter may change in a later version of the
program:

#include <stdlib.h>
struct opertable *t = malloc(sizeof *t);

And then check for malloc failure.
t->delegate( someargumenttype arg );


OK, if the definition of struct opertable actually has something like

int (*delegate)(someargumenttype);
How then is it possible to do something like this:

int delegate_impl ( someargumenttype arg ) {
// do something with arg, parse etc.
printf ( "Called delegate() from within : %s\n",
<reference_on_containingstruct>->operatable_id
);
}


This is not entirely clear. If you intend to use delegate_impl as
a possible value for your delegate field, then it will not have
access to the struct opertable * variable through which it was
called. If you want such access, you'll have to pass it. For
example:

struct operatable
{
char *operatable_id;
int (*delegate)(struct opertable *, somedatatype);
somedatatype data;
};

int delegate_impl(struct opertable *caller, somedatatype arg)
{
/* ... */
printf("Called delegate from %s\n", caller->opertable_id;
return 0;
}

By the way, putting whitespace between the "*" and the identifier
name when specifying pointer types is a bad idea. It can lead to
errors like:

char * pointer1, pointer2;

where "pointer2" is in fact a char, not a char *, despite its name.


But that's not an error. It defines a variable that can hold a memory
address to
a char and a variable that holds a char. I don't exactly know the
C-Coding Guidelines, but i prefer to put some space between all tokens
in order to
render the code more readable. Ok, it may mislead. With this, i agree.

To the main problem: i wanted to return to C-Style for developing a
some server-core-components. Coming from Perl/Java, i want to test the
boundaries of
the C-Language-functionalities. (Java to C because Java cannot be
sufficiently observed, from the systems administrator's point of view.
How many java apps(server apps) must be rebooted due to misallocated
JVM memory? But this is offtopic.)

I have to (re)reorient a bit, when returning to C.

So, in a procedural language like c i have to achieve that kind of
functionality
by passing args to the function.

Thanks for the hints.
Ole
Nov 14 '05 #4

In article <4b**************************@posting.google.com >, ov*@gmx.com (Ole) writes:
mw*****@newsguy.com (Michael Wojcik) wrote in message news:<cl*********@news3.newsguy.com>...
In article <4b**************************@posting.google.com >, ov*@gmx.com (Ole) writes:
When i now have designed a function to int (*delegate)() and i call it
You mean you've assigned a value to "delegate"?

Yes. I meant to pass a function pointer to the "delegate" variable.

By the way, is

int ( * delegate ) ( int x, ... );

still a variable? I would say it is, since pointers are only variables
that can hold memory a address.


It declares a variable named "delegate" of type "pointer to function
taking an integer and a variadic argument list and returning an int".
In other words, I think the answer to your question is "yes", more or
less. You're correct that pointers, including function pointers, are
variables.
By the way, putting whitespace between the "*" and the identifier
name when specifying pointer types is a bad idea. It can lead to
errors like:

char * pointer1, pointer2;

where "pointer2" is in fact a char, not a char *, despite its name.


But that's not an error. It defines a variable that can hold a memory
address to a char and a variable that holds a char.


Ah, but it is an error. Source code has two audiences: programmers
and language implementations. It must speak accurately to both.
Even if this fragment contains no C errors, it still contains an
error in its representation of the program to programmers, because
the name of the "pointer2" variable belies its type.
I don't exactly know the
C-Coding Guidelines,
There are guidelines and guidelines; none have any force other than
opinion, but some of them are better-informed and justified opinions
than others. Agreement is fairly widespread on this particular
point (not separating the "*" and the identifier name when declaring
pointers).
To the main problem: i wanted to return to C-Style for developing a
some server-core-components. Coming from Perl/Java, i want to test the
boundaries of the C-Language-functionalities.

I have to (re)reorient a bit, when returning to C.

So, in a procedural language like c i have to achieve that kind of
functionality by passing args to the function.


That's correct. Typically OO languages "invisibly" pass a reference
to the calling object to the method it invokes. In C you have to do
that explicitly, because the language doesn't do it for you. Simple
as that.

If you'd prefer a language with built-in OO support, there's always
C++, of course. Just like C except for a handful of subtle differences
and several tons of baggage piled on top. Combines C's safety and
ease of use with Perl's clarity and maintainability.

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

There are many definitions of what art is, but what I am convinced art is not
is self-expression. If I have an experience, it is not important because it
is mine. It is important because it's worth writing about for other people,
worth sharing with other people. That is what gives it validity. (Auden)
Nov 14 '05 #5

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

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
8
by: Martin Marcher | last post by:
Hi, I'm working on a program that creates a linear list of structs struct lin_list{ struct lin_list *next; char Name; }; this is what it looks like. And i got (design) problems with the...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.