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

Specifying data types inside functions

Here is my problem:

I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:

I have the following two data types:

typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;

Thus, I know that both data_one and data_two have an integer field, x.
The remaining fields can be arbitrarily different.

I want to have a function that takes as an argument a pointer to one of
those structures, increments the x member and returns a pointer to the
same structure. Currently I have two functions:

data_one * f_one(data_one * data) data_two * f_two(data_two * data)
{ {
data->x += 1 ; data->x += 1 ;

return data ; return data ;
} }

The bodies of the functions are identical - which is a waste.
I'd rather use a single function, with a prototype similar to:

void * f(data_type type, void * data)

where "type" would specify the actual type of the data we are dealing with.

Can something along these lines be done? Please remember that the above
is just a toy example.




Nov 14 '05 #1
4 1251

"No One" <No******@nowhere.net> wrote in message
I have the following two data types:

typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;

I'd rather use a single function, with a prototype similar to:

void * f(data_type type, void * data)

where "type" would specify the actual type of the data we are
dealing with.

You very rapidly get into a murky area of the C standard here. There are
some guarantees about structure layout in memory, but it is generally not a
good idea to use extendable structures or to try to convert one type of
structure to another.

If two structures have a common header then do this

struct common
{
int x;
...
};

typedef struct
{
struct common header;
...
}data_one;

typedef struct
{
struct common header;
...
} data_two;

Now when you write f

void f(data_type type, void *data)
{
data_one *data1;
data_two *data2;
switch(type)
{
case 1:
data1 = data;
foo( data1->header);
break;
case 2:
data2 = data;
foo(data2->header);
break;
}
}

Theoretically you could cast data to a struct common * directly, but this is
asking for trouble. For instance, if someone inserts another member at the
beginning of one of the structs the code will break without warning.
Nov 14 '05 #2
On Sat, 10 Apr 2004 19:39:38 +0100, Malcolm wrote:

You very rapidly get into a murky area of the C standard here. There are
some guarantees about structure layout in memory, but it is generally not
a good idea to use extendable structures or to try to convert one type of
structure to another.

If two structures have a common header then do this

struct common
{
int x;
...
};

typedef struct
{
struct common header;
...
}data_one;

typedef struct
{
struct common header;
...
} data_two;

Now when you write f

void f(data_type type, void *data)
{
data_one *data1;
data_two *data2;
switch(type)
{
case 1:
data1 = data;
foo( data1->header);
break;
case 2:
data2 = data;
foo(data2->header);
break;
}
}
}
Theoretically you could cast data to a struct common * directly, but this
is asking for trouble. For instance, if someone inserts another member at
the beginning of one of the structs the code will break without warning.


Thanks for your feedback. The solution that you propose is similar to
what I am using. My actual problem is even worse than I presented, for the
common fields might not always be the first ones in the structures :-( It
seems to me that I am stuck with ugly code.

Nov 14 '05 #3
No One wrote:
Here is my problem:

I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:

I have the following two data types:

typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;

Thus, I know that both data_one and data_two have an integer field, x.
The remaining fields can be arbitrarily different.

I want to have a function that takes as an argument a pointer to one of
those structures, increments the x member and returns a pointer to the
same structure. Currently I have two functions:

data_one * f_one(data_one * data) data_two * f_two(data_two * data)
{ {
data->x += 1 ; data->x += 1 ;

return data ; return data ;
} }

The bodies of the functions are identical - which is a waste.
I'd rather use a single function, with a prototype similar to:

void * f(data_type type, void * data)

where "type" would specify the actual type of the data we are dealing with.

Can something along these lines be done? Please remember that the above
is just a toy example.


You can use the preprocessor.

#define func(type) type* f_##type (type* data) {\
data->x+=1; \
return data; \
}

func(data_one)
func(data_two)
With C++, you'd use templates.

-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #4

Where is Cheema these days?

--
Munawar Akram
-=-
This message was posted via two or more anonymous remailing services.


Nov 14 '05 #5

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

Similar topics

2
by: Enigman O'Maly | last post by:
I'm still somewhat new to object style programming (as will become evident), using VBA in Excel 2000 to automate some previously manual functions. I've defined a class module so that I can...
21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
4
by: No One | last post by:
Here is my problem: I have a certain set of well-defined manipulations that I have to apply to different types of data. In all cases the manipulations are exactly the same, and are to be...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
41
by: laimis | last post by:
Hey guys, I just recently got introduced to data mappers (DTO mapper). So now I have a SqlHelper being used by DTOMapper and then business layer is using DTOMapper when it needs to persist...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
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: 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?
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...
0
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,...
0
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...
0
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,...
0
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...

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.