Computing.Net > Forums > Programming > Type casting void pointers

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Type casting void pointers

Reply to Message Icon

Name: Dr. Nick
Date: February 23, 2005 at 17:25:39 Pacific
OS: WinXP Pro SP1a
CPU/Ram: P4 2.0Ghz / 1024MB
Comment:

I'm fairly familiar with pointers in C++, but ran into a problem today. I have a function that I want to be able to take an input of any datatype. As an example, a simple one to find the index of an item in an array:

=================================================================

int find(void *array, void *item, int sizeofArray)
{
  for (int i=0; i<sizeofArray; i++)
    if (array[i] == *item)
      return i;

  return -1;
}

The problem with this is that the compiler has no idea what the size of the void pointers are. I thought I could fix this by adding another parameter to the function, then based on that, cast the void pointers to a definite type:


int find(void *array, void *item, int sizeofArray, int sizeofData)
{
  switch(sizeofData)
  {
    case sizeof(int): // Convert void* to int*
    case sizeof(char): // Convert void* to int*
    // ...
    default: return -1;
  }

  for (int i=0; i<sizeofArray; i++)
    if (array[i] == *item)
      return i;

  return -1;
}

The problem above is that I can't figure out how to either completely change the type of the pointers 'array' and 'item', or create a new set of pointers which will let the code work.

Any ideas? (Or questions as to what the heck I'm talking about more likely...)

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: February 24, 2005 at 04:35:20 Pacific
Reply:

I don't know about casting a void to something else, but why not just overload the function to allow for the finite number of possibilities?

Chi

They mostly come at night...mostly


0

Response Number 2
Name: egkenny
Date: February 24, 2005 at 10:00:09 Pacific
Reply:

You might want to consider using templates. That way the data type can be any numeric date type. Here is an example:

template<class T> int find(T *array, T *item, int sizeofArray)
{
for (int i=0; i<sizeofArray; i++)
if (array[i] == *item)
return i;
return -1;
}

I believe the syntax is correct. I can't test it right now but post back if you have any problems or questions.

It could be modified to handle any data type if the comparison operator "==" was overloaded to handle the comparison. For example if the data type was a struct or class your would need to define "operator==" as one of the methods.


0

Response Number 3
Name: Dr. Nick
Date: February 24, 2005 at 10:00:23 Pacific
Reply:

I guess I could do that, but it means I'll have something like 4 or 5 identical function bodies, with the exception of a single line. I was hoping I could avoid that seeing as duplicate code is such a bad thing :)

Besides, plenty of other functions use void pointers and somehow cast them to be used as they see fit.

Thanks.


0

Response Number 4
Name: Dr. Nick
Date: February 24, 2005 at 10:16:07 Pacific
Reply:

Oops, I was replying to Chi's post.

egkenny - I'll try your code out tonight.

Thanks.


0

Response Number 5
Name: egkenny
Date: February 24, 2005 at 17:27:09 Pacific
Reply:

Notes:
1. For templates the keywords typename and class are interchangable here.

2. If the types are different enough then C++ can figure out what types you are using. Note that the types "int" and "unsigned char" were close enough that I had to specify which one to use.

For example type not specified but implied:
find(doubArray, doubItem, size)

For example types had to be specified because C++
could not distiguish between them:
find<int>(intArray, intItem, size)
find<unsigned char>(ucharArray, ucharItem, size)

Here is a sample program:

#include <iostream>
using namespace std;

template<typename T>
int find(T array[], T item, int sizeofArray)
{
for (int i=0; i<sizeofArray; i++)
if (array[i] == item)
return i;
return -1;
};

int main()
{
int intArray[] = {45, 34, 11, 8, 134}, intItem = 34;
double doubArray[] = {45.0, 34.0, 11.0, 8.0, 134.0}, doubItem = 8.0;
unsigned char ucharArray[] = {45, 34, 11, 8, 134}, ucharItem = 11;
int size = 5;

cout << "int find = " << find<int>(intArray, intItem, size) << endl;
cout << "doub find = " << find(doubArray, doubItem, size) << endl;
cout << "uchar find = " << find<unsigned char>(ucharArray, ucharItem, size) << endl;
}

Output:

int find = 1
doub find = 3
uchar find = 2


0

Related Posts

See More



Response Number 6
Name: Dr. Nick
Date: February 24, 2005 at 18:20:41 Pacific
Reply:

Oh, I see you posted again. Thanks for that, though I had already got it.

You can see the fruits of my (thanks to your) labors at this question.

Thanks again. I'd never used templates in C++, but wow, are they beautiful! :)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Type casting void pointers

type casting www.computing.net/answers/programming/type-casting/1988.html

type casting VC++ .NET www.computing.net/answers/programming/type-casting-vc-net/11977.html

about type casting www.computing.net/answers/programming/about-type-casting/1515.html