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

Array of pointers to different types

Hello, gurus. I spent an hour looking for the answer to this and
couldn't find one that pleased me (I don't want to use a base class,
which seems to be a solution).

So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

I need to pass the following data to the function:

vector<CustomStruct1list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

void * array[4];
array[0] = &list;
array[1] = &sixstructmembers;
array[2] = &p1;
array[3] = &p2;
So now "array" should contain 4 pointers, each to a different data
structure.

My problem is dereferencing the pointers. When I get into the function,
I want to do something like
double * p2 = (double*)array[3] but that doesn't work, I'm guessing
because the compiler doesn't know what [3] means in this sense (no size
for void*). How do I work around this?

After thinking too much about it, I'm getting lost in all the
indirection. Is it a valid idea to use int * array[4];
array[0]=(int)&list; and then cast the array in the function as int *
newarray = (int*)array? Somehow that seems a little hackish.

Dec 12 '06 #1
5 5188

whiskers wrote:
Hello, gurus. I spent an hour looking for the answer to this and
couldn't find one that pleased me (I don't want to use a base class,
which seems to be a solution).

So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

I need to pass the following data to the function:

vector<CustomStruct1list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

void * array[4];
array[0] = &list;
array[1] = &sixstructmembers;
array[2] = &p1;
array[3] = &p2;
So now "array" should contain 4 pointers, each to a different data
structure.

My problem is dereferencing the pointers. When I get into the function,
I want to do something like
double * p2 = (double*)array[3] but that doesn't work, I'm guessing
because the compiler doesn't know what [3] means in this sense (no size
for void*). How do I work around this?

After thinking too much about it, I'm getting lost in all the
indirection. Is it a valid idea to use int * array[4];
array[0]=(int)&list; and then cast the array in the function as int *
newarray = (int*)array? Somehow that seems a little hackish.
Hey Whiskers, how about try something like this, let me know if this
helps
#include <iostream>

void func(void* data)
{
void** array = static_cast<void**>(data);
std::cout << *static_cast<double*>(array[0]) << std::endl;
std::cout << *static_cast<double*>(array[1]) << std::endl;
std::cout << *static_cast<int*>(array[2]) << std::endl;
std::cout << *static_cast<int*>(array[3]) << std::endl;
}

int main(int argc, char** argv)
{
double d1 = 2.0, d2 = 3.0;
int i1 = 4, i2 = 5;

void* array[4];
array[0] = &d1;
array[1] = &d2;
array[2] = &i1;
array[3] = &i2;

func(array);

return 0;
}
-----
Ivan
http://www.0x4849.net

Dec 13 '06 #2
whiskers wrote:
So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

I need to pass the following data to the function:

vector<CustomStruct1list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)
The cleaner way will be something like the following:

struct MyThing
{
vector <CustomStruct1* list;
CustomStruct2 * sixstructmembers;
double p1;
double p2;
};

void cantchangethisdeclaration (void * array)
{
MyThing * thing (static_cast <MyThing *(array) );
// Do whatever you want with * thing
}

MyThing my;
my.list= & list;
my.sixstructmembers= & sixstructmembers;
my.p1= p1;
my.p2= p2;
cantchangethisdeclaration (& my);

Sure "array" is not used like an array, but who cares? (And if you care,
think of it as an array of one element).

By the way, the name "list" for a vector is confusing.

--
Salu2
Dec 13 '06 #3

Julián Albo wrote:
The cleaner way will be something like the following:

struct MyThing
{
vector <CustomStruct1* list;
CustomStruct2 * sixstructmembers;
double p1;
double p2;
};

void cantchangethisdeclaration (void * array)
{
MyThing * thing (static_cast <MyThing *(array) );
// Do whatever you want with * thing
}

MyThing my;
my.list= & list;
my.sixstructmembers= & sixstructmembers;
my.p1= p1;
my.p2= p2;
cantchangethisdeclaration (& my);
Yes this is a better solution if you are allowed to use it in your
requirements, but the one i posted should work too.
---
Ivan
http://www.0x4849.net

Dec 13 '06 #4

whiskers wrote:
Hello, gurus. I spent an hour looking for the answer to this and
couldn't find one that pleased me (I don't want to use a base class,
which seems to be a solution).

So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.
Then you essentially have code in C, not C++.
You should ask your question in a C newsgroup, sorry.
Its bad code, bad design. Not to mention downright dangerous and
time-consuming.

By the way, passing an array of any type, including your own types, is
simple, automatic and easy. It can also be safe - using a reference as
shown below. It also involves a very tiny amount of code.

#include <iostream>
#include <ostream>

template< typename T, const size_t Size >
void array_by_ref(T (&array)[Size])
{
for(size_t i = 0; i < Size; ++i)
{
std::cout << array[i] << std::endl;
}
}

int main()
{
double darray[10] = {0};
array_by_ref(darray);
}

/*
0
0
0
0
0
0
0
0
0
*/

And you can change double in that array to anything copyable.
>
I need to pass the following data to the function:

vector<CustomStruct1list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

void * array[4];
array[0] = &list;
array[1] = &sixstructmembers;
array[2] = &p1;
array[3] = &p2;
So now "array" should contain 4 pointers, each to a different data
structure.

My problem is dereferencing the pointers. When I get into the function,
I want to do something like
double * p2 = (double*)array[3] but that doesn't work, I'm guessing
because the compiler doesn't know what [3] means in this sense (no size
for void*). How do I work around this?

After thinking too much about it, I'm getting lost in all the
indirection. Is it a valid idea to use int * array[4];
array[0]=(int)&list; and then cast the array in the function as int *
newarray = (int*)array? Somehow that seems a little hackish.
Dec 13 '06 #5
Julián Albo wrote:
struct MyThing
{
vector <CustomStruct1* list;
CustomStruct2 * sixstructmembers;
double p1;
double p2;
};

void cantchangethisdeclaration (void * array)
{
MyThing * thing (static_cast <MyThing *(array) );
// Do whatever you want with * thing
}

The idea of a wrapper struct somehow isn't appealing to me, because it
seems like another thing to clutter the code. In general, however, how
is it looked upon? Is it a preferable thing in cases like this?
Ivan Novick wrote:
Yes this is a better solution if you are allowed to use it in your
requirements, but the one i posted should work too.
---

I works perfectly, just what I wanted! The idea of an array of pointers
to objects seemed totally logical, but I just wasn't going deep enough
with the dereferencing.
Thanks, guys!

Dec 13 '06 #6

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
18
by: Joshua Neuheisel | last post by:
The following code compiles with gcc 3.2.2 and Visual C++ 6: #include <stdio.h> int main() { int a = {3, 4}; printf ("%d\n", 0); return 0; }
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
9
by: sangeetha | last post by:
Hello, Is there any performance difference in using of the following two declaration? int (*ptr); //Array of 10 int pointers int *ptr; // pointer-to-array of 10. Regards, Sangeetha.
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.