Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9287697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:44:23+00:00 2026-06-18T19:44:23+00:00

Alright so i’ll try to explain my problem clearly. I would like to have

  • 0

Alright so i’ll try to explain my problem clearly.

I would like to have a function which would sort an array of anything, based on the size of an element and on the offset and the size of the the variable in an element (for use with structures).

So my function would look like:

void sort(void* array, size_t elem_size, int elem_count, size_t operand_offset, size_t operand_size)

array is a pointer to the beginning of the array

elem_size is the size of one element in the array

elem_count is the count of elements in the array

operand_offset is the offset of the variable within the element to base the sort on (it would be 0 if the element is only one variable, but it could be more if the element is a struct)

operand_size is the size of that variable

In this function i need to create a temp variable, i would do it like that:

void* temp = malloc(elem_size);
*temp = *(array+ i*elem_size);

but the compiler doesn’t agree: dereferencing void* pointer, and he doesn’t know the size of my temp variable…

I know i could do it byte per byte, but i would like to know if there is a better way.

So my question is:
How to set the “size” of a pointer to elem_size ?

Aditional question:
Could i type array[i] to access an element if the size is known ?


EDIT
Ok so my problem is solved i must use memcpy

But now i have another problem i didn’t expect.

Given the size and the offset of the operand within the element, how could i extract it and compare it?

kinda like:

void *a = malloc(operand_size);
void *b = malloc(operand_size);
memcpy(a, array+i*elem_size + operand_offset, operand_size);
memcpy(b, array+j*elem_size + operand_offset, operand_size);

if (a < b)
...
else
...

How can i do that?


*EDIT 2: *
Well, finally it was too complicated managing an if statement for each size of operand, so i did something completely different

So basically i had a void *array containing n elements, and i was writing a function to sort it.

But instead of giving directly the offset and the size of the operand within the element, i gave the function another function to compare two elements. That works well

int compareChar(void* a, void* b);
int compareShort(void* a, void* b);
int compareInt(void* a, void* b);
int compareLong(void* a, void* b);
int compareFOO(void* a, void* b);

void sort(void* array, size_t elem_size, int elem_count, int (*compare)(void*,void*));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:44:24+00:00Added an answer on June 18, 2026 at 7:44 pm

    Couldn’t you just use memcpy? That will likely do it in the most efficient manner.

    uint8_t* temp = malloc(elem_size);
    memcpy(temp, array + i * elem_size, elem_size);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright i have a table which contains 2 bigint column. Example val1 3213 12312
Alright so here is my problem. Basically I have a string with 4 words
Alright so basically I have 3 threads. Each one of them use a function
Alright so I have no idea how to even begin doing this But basically
Alright so I have been looking around (on SO and Google) to see if
Alright, I'll try and make this brief as possible. I wanted to a UIToolbar
Alright. I'm not sure how to explain this correctly, and the title of this
Alright so here is my mysql function DELIMITER $$ CREATE DEFINER=`root`@`%` FUNCTION `password_reset_request`(spUsername VARCHAR(120))
Alright I have been looking for this for a while now, and didn't really
Alright, so I have done the following: I've added objects to an ArrayList and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.