473,799 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass various structures to function?

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 #1
12 3847
Fred 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:
myfunc(char * str, struct astruct *as)
From googling, it always appears that one specific
structure is passed to the function.
Use the facilities of <stdarg.h>. Remember that you need at least one
named argument after which the variable argument list occurs. In your
case the poorly-named char *str could be used to pass information about
the number and types of arguments, as does the format string for the
printf() and scanf() families. Your example uses pointers-to-structs
which is a good idea if you are mixing differently defined struct
arguments as well as changing their number.

I would, however, suggest that you start by learning how to use variable
argument lists with the arguments being drawn from the fundamental
integer and floating types before moving on to more complex arguments
and argument lists.
May 3 '07 #2
In article <rv************ *************** ***@comcast.com >,
Fred <it****@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?
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".
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
There was a twit named Ross St John, who everyone wished would be gone.
He called himself Cass, We plonked him en-masse,
And let out a c-l-c yawn. --Dann Corbit in comp.lang.c
May 3 '07 #3
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".
dave


I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.

May 3 '07 #4
On May 2, 10:04 pm, 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:

myfunc(char * str, struct astruct *as)

From googling, it always appears that one specific
structure is passed to the function.

-TIA
A void * argument will accept a pointer to any data type. However, it
could just as easily be passed a char[] or an int as a struct, so if
you want to catch those possibilities, this wouldn't work for you. And
there's no way to retrieve the original type directly -- it could be
done with an extra parameter and selection logic.

May 3 '07 #5
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

struct a { int whiz; char *name; };
struct b { char *this, *that; double whiz; }

.... 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) {
...
}

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.

--
Eric Sosman
es*****@acm-dot-org.invalid

May 3 '07 #6
Fred said:
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.
How will the function know what kind of structure you're passing it? It
will need this information, in order to know how to conduct the search.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 3 '07 #7
Richard Heathfield wrote, On 03/05/07 05:33:
Fred said:
>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.

How will the function know what kind of structure you're passing it? It
will need this information, in order to know how to conduct the search.
Unless the key to be searched on is a common initial sequence to all the
structure types of interest.

Without knowing more we can't say what the best solution is, that much
is certain.
--
Flash Gordon
May 3 '07 #8
On 3 May, 04:31, Fred <itf...@cdw.com 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".
dave

I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.
how will your function know where to look for the data?

What do the structs look like? What does the data look like?

--
Nick Keighley

May 3 '07 #9

"Fred" <it****@cdw.com a écrit dans le message de news:
rv************* *************** **@comcast.com...
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
Such a polymorphism is available in c : lool at stdarg.h
I suppose you will use the first argumeny as a type identifier?...

Xavier
May 3 '07 #10

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

Similar topics

9
2555
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...
8
1575
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
6
5559
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?
0
1352
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...
1
1382
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.
1
2556
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
2
3024
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
2
1720
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...
12
11115
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...
0
10482
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...
1
10225
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
9072
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
7564
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
6805
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
5463
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...
1
4139
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
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.