9 Replies - 619 Views - Last Post: 07 May 2011 - 10:38 PM Rate Topic: -----

#1 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 06:23 PM

is there anyway to compare 2 elements within an void array without using typecasting? i am given a comparator function that will take 2 void elements, but the element is going to come out of the void array. if i don't typecast how am i even able to retrieve the content of the array to even begin the comparison?... will someone tell me if it is possible? if it isn't possible, should i just type cast the void array into unsigned int array so i can work with it? thanks a bunch. this is C btw.

This post has been edited by obviousninja: 07 May 2011 - 06:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: is there anyway to compare 2 elements within an void array without typ

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 06:36 PM

Dereferencing a void pointer without a cast is not allowed.

What is the array actually holding?
Was This Post Helpful? 1
  • +
  • -

#3 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 06:39 PM

View PostKYA, on 07 May 2011 - 06:36 PM, said:

Dereferencing a void pointer without a cast is not allowed.

What is the array actually holding?


the array can hold anything ranging from string, int, long, etc.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 06:46 PM

Then why would you type cast to an unsigned int?

How are you assigning the void pointers to the elements? How are the original items allocated?
Was This Post Helpful? 1
  • +
  • -

#5 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 06:52 PM

i thought if i just typecast it to unsigned int i will at least be able to work with it. albeit that i may lose some data if the data size is larger than unsigned int, and i will also be able to do some comparison, but... i really don't think i am suppose to do that. that's why i am completely confused.

This post has been edited by obviousninja: 07 May 2011 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 06:58 PM

i also try to split the void array by pointer arithmetic, but that doesn't work does it?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 07:01 PM

Are the items in a void[] or did you point a bunch of void* at stuff? You could compare the addresses stored by the pointers, which would indicate they are pointing at the same thing in memory, but that's about it.
Was This Post Helpful? 1
  • +
  • -

#8 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 07:09 PM

the actual syntax is void *arr. so i guess i would have to resort to typecasting. wow, confused. its no good if i can only check to see if one item is the same as next. i would also have to split the array in two and do merge sort with it.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 07:19 PM

Can I see how you're initializing/filling void* arr?
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: is there anyway to compare 2 elements within an void array without typ

Posted 07 May 2011 - 10:38 PM

I'm having trouble seeing the issue here:

int comp(void * a, void * B)/> { return  *(int *)a > *(int *)b; }

void sort(void **arr, int (*cmp)(void*, void*)) {
	/* e.g. */
	comp(arr[0], arr[1]);
}



Is this what you are talking about?

If that's not the function signatures you are working with, post the exact signatures you have to work with.

This post has been edited by Oler1s: 07 May 2011 - 10:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1