void* array

Let's say that i get a void* array passed to a my function, and the size of the type of each element of the array:

void foo(void* array, int size, int len){
...
}

How do I access to the index element of the array??

If I want to access, let's say,  to the array[20], how do i do knowing only the size of each element but not the type to do a casting?
LVL 3
RomanRegaAsked:
Who is Participating?
 
Joseph2002Connect With a Mentor Embedded EngineerCommented:
OK, I think I now understand better your problem.

mysort() doesn't know the type of the elements, but it DOES know the size. By knowing the size it is easy to point to the addresses of two given elements of the array, isn't it? Let's say you want to compare the element of index 20 with the one of index 21. You could prepare the two pointers to be passed on to your CMP function like this:

char *ptr1;
char *ptr2;
ptr1 = (char*)array + (size * 20);
ptr2 = (char*)array + (size * 21);

Now you could establish that ALL of your CMP functions, regardless of the type they deal with, return NOT the difference between the two elements, BUT one of three values that just show the relation between them, eg:

0 if the two elements are equal
1 if the the first element is greater than the second
-1 if the second element is greater than the second

By so doing, your mysort() function will only know what it needs to know, that is whether the two elements are the same or which one is greater, so as to swap them if necessary. To swap the elements knowing only their size is possible, since all you need to do is call memcpy() using a temporary buffer.

Let's say the CMP() returned -1 for the two elements at position 20 and 21 of the array. You then know you have to exchange the two elements between them. You define a temporary buffer in mysort():

#define MAX_SIZE_EACH_ELEMENT 16
char tmp[MAX_SIZE_EACH_ELEMENT];

// to swap the two elements pointed to by ptr1 and ptr2
memcpy( tmp, ptr1, typesize );
memcpy( ptr1, ptr2, typesize );
memcpy( ptr2, tmp, typesize );

Perhaps I went too deeper and wrote things that you might know very well, sorry about that. But tell me if I still don't get your point.

Regards.
0
 
ddunleaCommented:
Hi RomanRega,

If you know the lenght of the element, but not the type, then you will be quite limited in what you can do. You can cast an element to be a byte array which will have "size" elements in it, and you can then access these individual bytes. But without knowing the actual type of the data, the contents of the bytes will most likely not be meaningful.

Can you give any more information e.g. why you don't know the type in advance and what you wish to do with the data when you receive it?

Regards
0
 
vashukumarCommented:
Type cast your array to the kind of strucure you are using.
Then it will automatically increment the poniter to point to the next elemnt i.e
if you incement the pointer , it will automatically point to the next element of the array.
you can access elements in this manner

(*apts)[iCurrentIndex+1]


0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
ddunleaCommented:
Hi vashukumar,

He doesn't know the type, so he can't perform the cast.
0
 
furqanchandioCommented:
hi romanrega

void foo(void* array, int size, int len){

in the function above size i think is the size in bytes of one element in the void* array and len is the number of elements in the array

so u can

int index;
for (int index=0; index<len; index+=size)
printf(" %d",array[index]);

or it can be vice versa

int index;
for (int index=0; index<size; index+=len)
printf(" %d",array[index]);
0
 
RomanRegaAuthor Commented:
I'm writing a simple sorting algoritm.
It should be able to be used whatever is the type of the objects to be sorted.

To do so the declaration will be something like this

typedef int(*COMPARE)(const void* , const void* );

void mysort(void* v, COMPARE cmp, int typesize, int start, int end){
...
}

cmp is a simple function to compare two elements,.
As you see the function is meant to work whatever is the type involved. THat's way i can't do casting in the usual way.

The question is:
Inside mysort, how can i access a element of the array by index, knowning only the typesize, but not the actual type.
0
 
ddunleaCommented:
Hi RomanRega,

This is not the way to do it. You want to use templates. If don't know the type of data, then there's no way you can perform a meaningful comparison.

This is a good place to start looking:
http://www.devarticles.com/c/a/Cplusplus/An-Introduction-to-C-plus-Templates/1/

Regards
0
 
Joseph2002Embedded EngineerCommented:
OK, perhaps it is better for me to return to your original example:

void foo(void* array, int size, int len){
...
}

and your question:
If I want to access, let's say,  to the array[20], how do i do knowing only the size of each element but not the type to do a casting?


I guess that "size" is the size of each element expressed as "number of bytes" (like the sizeof() operator). Now, if you want to access the array[20] element you just define a byte pointer, let's say like this:

char *ptr;

and then point it to the address of your element like this:

ptr = (char*)array + (size * 20);

This is the way your "compare" function can access the two array to compare two elements and see whether they are equal or not. But this function too must know the size of each element, so you probably have to rewrite its prototype this way:

typedef int(*COMPARE)(const void*, const void*, int);

where the third parameter is the "typesize" that your mysort function knows and will pass on to the "cmp" function.

I am not sure I completely grasped the meaning of your question, but I hope that will help.
0
 
RomanRegaAuthor Commented:
>> typedef int(*COMPARE)(const void*, const void*, int);
>> where the third parameter is the "typesize" that your mysort function knows and will pass on to the "cmp" function.

Actally this is not completely accurate. The COMPARE functun HAS TO KNOW the actual type.
Is the mysort function which doesn't know.

That's why i pass the function as a parameter. It's impossible in fact to compare two element not knowning the type.
If the type is int, it will be like so

int IntCompare(const void * a, const void * b){
    return *(int*)a-*(int*) b;//as you see here I must know the type
}

If the type is String, instead:

int StringComare(const void * a, const void * b){
    return strcasecmp( (char*)a, (char*) b);//as you see here too I must know the type
}


All I want is to do get something similar to the qsort() of the <stdlib.h> library, and simply write my own:

int IntCompare(const void * a, const void * b){
    return *(int*)a-*(int*) b;//as you see here I must know the type
}

int main(int argc, char *argv[])
{
     int len=20;
     int v[len];
     srand( (unsigned)time( NULL ) );          
     for (int i=0; i<len; i++){
         v[i]=rand()%20;
     }    
     
     qsort( v, len, sizeof(int), IntCompare);//<<<<<<<<<<<-----------------------------------------LOOK HERE
     // mysort( v, len, sizeof(int), IntCompare);//here there will be my own sorting function in the place of the native qsort()
     
     for (int i=0; i<len; i++){
        printf("%d %d\n",i, v[i]);  
     }    
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


0
 
RomanRegaAuthor Commented:
Thanks :)
0
All Courses

From novice to tech pro — start learning today.