473,409 Members | 2,056 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,409 software developers and data experts.

Having a generic function

Afternoon everyone. I am having some trouble and thought a few of you
might be able to help me... I have a simple function to copy the
contents of one array into another - the arguments to the function are
the first array (to copy into), the second array (copy from) and the
number of items to copy, the prototype is void cpyarray (float * a,
float * b, int numbertocopy) - this worked fine initially.

However, I would now like to use this function to, not only copy
contents of float array, but also double array (and in the future
other types such as int arrays, char array etc...) As this is the
case, is there any way I can change the type declaration of the two
arrays in the fn prototype so that it simply copys them, without
casting to or expecting a specific type? I tried void * a, void *b ..
but that did not work

Cheers,
Nick

Oct 4 '07 #1
5 1418
polas wrote:
Afternoon everyone. I am having some trouble and thought a few of you
might be able to help me... I have a simple function to copy the
contents of one array into another - the arguments to the function are
the first array (to copy into), the second array (copy from) and the
number of items to copy, the prototype is void cpyarray (float * a,
float * b, int numbertocopy) - this worked fine initially.
What's stopping you using memcpy() or memmove()?
Oct 4 '07 #2
polas wrote:
Afternoon everyone. I am having some trouble and thought a few of you
might be able to help me... I have a simple function to copy the
contents of one array into another - the arguments to the function are
the first array (to copy into), the second array (copy from) and the
number of items to copy, the prototype is void cpyarray (float * a,
float * b, int numbertocopy) - this worked fine initially.

However, I would now like to use this function to, not only copy
contents of float array, but also double array (and in the future
other types such as int arrays, char array etc...) As this is the
case, is there any way I can change the type declaration of the two
arrays in the fn prototype so that it simply copys them, without
casting to or expecting a specific type? I tried void * a, void *b ..
but that did not work
You cannot directly deference void *. It must be cast to a type before any
deferencing. In your case an extra parameter to the function could let the
caller specify the types of the arrays, with which you can cast the void *
to the appropriate types before copying.

You can also specify that the function accepts unsigned char *. This would
imply more work for the calling function though.

Oct 4 '07 #3
On 4 Oct, 13:17, santosh <santosh....@gmail.comwrote:
polas wrote:
Afternoon everyone. I am having some trouble and thought a few of you
might be able to help me... I have a simple function to copy the
contents of one array into another - the arguments to the function are
the first array (to copy into), the second array (copy from) and the
number of items to copy, the prototype is void cpyarray (float * a,
float * b, int numbertocopy) - this worked fine initially.
However, I would now like to use this function to, not only copy
contents of float array, but also double array (and in the future
other types such as int arrays, char array etc...) As this is the
case, is there any way I can change the type declaration of the two
arrays in the fn prototype so that it simply copys them, without
casting to or expecting a specific type? I tried void * a, void *b ..
but that did not work

You cannot directly deference void *. It must be cast to a type before any
deferencing. In your case an extra parameter to the function could let the
caller specify the types of the arrays, with which you can cast the void *
to the appropriate types before copying.

You can also specify that the function accepts unsigned char *. This would
imply more work for the calling function though.
Thanks for the help both of you - I have tried both methods and they
both work well :) - as for the memcpy I completely forgot that I could
use that

Oct 4 '07 #4
polas wrote:
>
Afternoon everyone. I am having some trouble and thought a few of
you might be able to help me... I have a simple function to copy
the contents of one array into another - the arguments to the
function are the first array (to copy into), the second array
(copy from) and the number of items to copy, the prototype is
void cpyarray (float * a, float * b, int numbertocopy) - this
worked fine initially.

However, I would now like to use this function to, not only copy
contents of float array, but also double array (and in the future
other types such as int arrays, char array etc...) As this is the
case, is there any way I can change the type declaration of the
two arrays in the fn prototype so that it simply copys them,
without casting to or expecting a specific type? I tried void
* a, void *b .. but that did not work
After careful examination of the detailed code you published, I
have concluded that the problem lies in line 42. Correct that.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 4 '07 #5
On Thu, 04 Oct 2007 04:58:40 -0700, polas wrote:
Afternoon everyone. I am having some trouble and thought a few of you
might be able to help me... I have a simple function to copy the
contents of one array into another - the arguments to the function are
the first array (to copy into), the second array (copy from) and the
number of items to copy, the prototype is void cpyarray (float * a,
float * b, int numbertocopy) - this worked fine initially.

However, I would now like to use this function to, not only copy
contents of float array, but also double array (and in the future
other types such as int arrays, char array etc...) As this is the
case, is there any way I can change the type declaration of the two
arrays in the fn prototype so that it simply copys them, without
casting to or expecting a specific type? I tried void * a, void *b ..
but that did not work
If the function's parameter has type void*, the argument is
converted to that type. The function cannot know what the type of
elements of the array are in the caller. It is the caller who has
to pass the size of them.
Try memcpy(dest, src, numbertocopy * sizeof *dest) directly,
without using another function. If you *need* to use another
function, pass the size of elements with another parameter.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 4 '07 #6

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

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
1
by: REH | last post by:
As I posted last week, I updated my C++ library to include a variant record class. While using it in some code, I'm finding it would be convenient to have a method that would basically say to the...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
6
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...
0
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...
0
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...

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.