Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:
myfunc(char * str, struct astruct *as)
From googling, it always appears that one specific
structure is passed to the function.
-TIA
May 3 '07
12 3848
On 5ÔÂ3ÈÕ, ÉÏÎç11ʱ04·Ö, Fred <itf...@cdw.com wrote:
Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:
I think you can do so:
void function(char *str, void *ptrList[])
{
/*
you can scan the content in str and determine which element in
ptrList should be used.
*/
}
/* usage of function: */
struct astruct as;
struct bstruct bs;
struct cstruct cs;
/* ... and so on */
void *ptrList[] = {&as, &bs, &cs/*, ... and so on */};
function(str, ptrList);
Sorry for my poor English. Hope my answer would give you some help.
Eric Sosman wrote:
Fred wrote:
>On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
>>What are you really trying to do?
I'm having trouble coming up with a case where the Right Answer is something other than "write a different function for each struct", possibly in the degenerate case of "use a single struct for all of the options".
I'm trying to search for data in a structure, and want to have 1 function that I can pass multiple structures to.
If the function doesn't know what the struct looks
like, how will it know where to search?
For example, let's say you have two structs
#define TYPE_A 1
#define TYPE_B 2
....
#define TYPE_Z 26
>
struct a { int whiz; char *name; };
struct b { char *this, *that; double whiz; }
struct a { int type; int whiz; char *name; };
struct b { int type; char *this, *that; double whiz; };
....
struct z { int type; float p; char *bar; int foo; };
>
... and pretend there's a magical way to get the function
to accept either of them[*]:
int myfunc(const *key, struct a_or_b *sptr) {
...
}
int myfunc(void *key, void *sptr)
{
switch (sptr->type)
{
case TYPE_A:
/* logic here */
break;
...
}
...
}
Be forewarned, however, that I'm not sure if "sptr->type" is legal. It
may be better to pass the structure type as an additional parameter
instead of making it a member of each structure. The function would be
(obviously) huge depending on the number of structure's however it may
be more maintainable than several functions which do (mostly) the same
thing. Again this all depends on the implementation and personally I
would prefer the different function approach whenever possible.
>
Knowing only that its second argument points to a struct a
or to a struct b, but not knowing which, what exactly are
you planning to have myfunc() do? [*] Actually, it's not so magical: There's a way to
pass a "pointer to anything at all" to a function, by using
a function parameter of type `void*'. But this leaves you
in exactly the same hole: You know that the argument points
at something, but you don't know what kind of a something
it points at. Unless you can deduce the nature of the
pointed-at thing from some other piece of information, the
things you can do with an "opaque" type are fairly limited.
On Thu, 03 May 2007 19:39:51 -0500, Joe Estock
<je*****@NOSPAM nutextonline.co mwrote in comp.lang.c:
Eric Sosman wrote:
Fred wrote:
On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote: What are you really trying to do?
I'm having trouble coming up with a case where the Right Answer is something other than "write a different function for each struct", possibly in the degenerate case of "use a single struct for all of the options".
I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.
If the function doesn't know what the struct looks
like, how will it know where to search?
For example, let's say you have two structs
#define TYPE_A 1
#define TYPE_B 2
...
#define TYPE_Z 26
Ugh, no, that's hideous and hard to maintain. Instead:
enum data_type {
TYPE_A,
TYPE_B,
/* etc. */
TYPE_Z
};
struct a { int whiz; char *name; };
struct b { char *this, *that; double whiz; }
struct a { int type; int whiz; char *name; };
struct b { int type; char *this, *that; double whiz; };
...
struct z { int type; float p; char *bar; int foo; };
No, let's keep it legal, defined C. Do this with Eric's structure
definitions:
struct data
{
enum data_type type;
union
{
struct a a;
struct b b;
/* etc. */
}
};
... and pretend there's a magical way to get the function
to accept either of them[*]:
int myfunc(const *key, struct a_or_b *sptr) {
...
}
int myfunc(void *key, void *sptr)
{
switch (sptr->type)
{
case TYPE_A:
/* logic here */
break;
...
}
...
}
Be forewarned, however, that I'm not sure if "sptr->type" is legal. It
No, it's not. But with the union approach:
int myfunc(struct data *data)
{
switch (data->type)
{
case TYPE_A:
/* yada, yada, yada */
may be better to pass the structure type as an additional parameter
instead of making it a member of each structure. The function would be
(obviously) huge depending on the number of structure's however it may
be more maintainable than several functions which do (mostly) the same
thing. Again this all depends on the implementation and personally I
would prefer the different function approach whenever possible.
Knowing only that its second argument points to a struct a
or to a struct b, but not knowing which, what exactly are
you planning to have myfunc() do?
[*] Actually, it's not so magical: There's a way to
pass a "pointer to anything at all" to a function, by using
a function parameter of type `void*'. But this leaves you
in exactly the same hole: You know that the argument points
at something, but you don't know what kind of a something
it points at. Unless you can deduce the nature of the
pointed-at thing from some other piece of information, the
things you can do with an "opaque" type are fairly limited.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: timothy.williams |
last post by:
Hi.
I trying to write an extension module to call some C libraries so I can
use them in Python. Several of the library functions pass pointers to
structures as arguments. I was thinking that I could create a class for
each structure, but I'm not sure how to get the data back and forth.
The example the "Extending and Embedding the Python Interpreter" manual
has examples of passing strings and ints using
PyArg_ParseTupleAndKeywords(), but...
|
by: Blue Ocean |
last post by:
I know this is somewhat dependent on the circumstances, but let me ask
anyway. Suppose I have a 100 byte struct or array or something like
that. Which would be more efficient?
void function(struct something foo)
{
foo.this + foo.that;
foo.somethingoranother++;
printf(foo.foostring);
//other operations on the array
|
by: yezi |
last post by:
Hi:
How to code for operation between 2 structures?
The structure's defination is :
struct sockaddr_in my_addr;
I need call a subfunction and pass the value in my_addr to the
subfuncition , which same structure but different name?
|
by: Pravin |
last post by:
I have following architecture.
c# code --> managed c++ code (wrapper) -> c wrapper code to third party
tool --> third party tool written in c.
c# code sends some data and a delegate to c++ code which is passed ahead to
c code and c code gives it ahead to third party tool. I am able to pass
integer, strings and structures to c code from c++ code. However, when I
attempt to pass a function pointer from managed c++ code to c code, it...
|
by: Kelvin |
last post by:
to All,
I'm a newbie in VC++, can anyone tell me about the
performance in various Data Structures (eg. Enum, map,
list, vector, hash, arrayList)?
I would like to know performance on CPU and Memory Usage.
eg. Mem usage may involve the mem usage on
creating/deleting an element in the structure.
| |
by: Scott McFadden |
last post by:
What is the proper way to pass pointers by reference from managed c++ calls
to native c++ calls?
In managed C++, I have a pointer to an array of structures, that I pass to a
native c++ method by reference:
//Managed c++
QueryResult* qr = NULL;
ExecuteQuery(1, "abc", qr); //invoke native c++ method passing qr by
reference
|
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 function. how
can I change my c# struct into a pointer that i could send to the c
function?
2. how can i send a null to this function that ecpect pointer to
|
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= |
last post by:
Have a complex process where I need to Import a large amount of data
then run some transformations on this data then import into DataBase.
The transformation involves multiple fields and multiple process - so the
data needs to be read in 1 record at a time then run thru the transformation
that may create new data value then everything is imported into a db to store.
I have multiple questions
1)we used to have an internal data structure...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I'm sure, but just didn't think this was my
problem; LOL, I am needling him)
If...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |