Hello ,

I wish to create a User-Defined array (1=float , 2=int) and to ALLOC
the array properly. I defined void* ARR in main function
and is a SWITCH (choise) i will make the desired array.

why doesnt it work?

Code is attached .

thanks :-)

Dani AI

Generated

As is trying to keep a single void* buffer for either ints or floats and find the two smallest values in one pass: yes — it’s doable, but the prototype you posted is missing the two pieces you need to walk a raw block of memory: the element size and the number of elements. As said, you must cast correctly when you access elements; beyond that, use a comparator that returns <0/0/>0 (not a raw subtraction) and do a single O(n) sweep that tracks the smallest and second-smallest.

Suggested, safer prototype (returns status and copies minima into caller buffers):

int two_min(const void *array, size_t n, size_t elem_size,
            int (*cmp)(const void *, const void *),
            void *out_min1, void *out_min2);

Safe comparator examples (don’t subtract integers — overflow risk):

int cmp_int(const void *a, const void *b) {
    int ia = *(const int*)a;
    int ib = *(const int*)b;
    return (ia < ib) ? -1 : (ia > ib) ? 1 : 0;
}

int cmp_float(const void *a, const void *b) {
    float fa = *(const float*)a;
    float fb = *(const float*)b;
    return (fa < fb) ? -1 : (fa > fb) ? 1 : 0;
}

One-pass approach (high level):

  • Treat the block as bytes: const char *base = array;
  • Initialize min1 = base (element 0), min2 = NULL.
  • For i = 1..n-1, compute cur = base + i*elem_size; compare with min1 and min2 using cmp.
  • If cur < min1: min2 = min1; min1 = cur.
  • Else if min2 == NULL or cur < min2: min2 = cur.
  • At end memcpy the two minima into out_min1/out_min2 (or return indices/pointers if you prefer).

Tips and cautions:

  • If you only have void** array (an array of pointers) iterate with void *elem = ((void**)array)[i] instead of byte arithmetic.
  • Check allocation for overflow: count <= SIZE_MAX / elem_size before multiplying.
  • Return status for n<2 so caller can handle missing second value.
  • For printing, cast the minima back to the correct type and use the right printf format.

This keeps the loop single-pass and safe across types while avoiding integer overflow and lifetime problems from returning pointers into temporary buffers.

Recommended Answers

All 5 Replies

a matter of correct typcasting. Note placement of asterisks and other typcasts in the scanf() line. I did not run your program so I don't know if it really works right.

case 1  : { user_array=(int*)malloc(size_of_input*(sizeof(int)));
	      if (user_array==NULL) { printf("MEM ALLOC FAILED\n"); exit(1); }
		 else
		   {
		   printf("Enter Data to Array\n");
		   for (loop=0;loop<size_of_input;loop++)
			scanf("%d",&((int*)user_array)[loop]); }
	      }

thank you :-)
follow up question - if i wish to print out the array , do i need typecasting as well , like in Scanf ?

yes -- how else can the program pass the correct data type to printf(). There is another solution -- create a union of data types than an array of union objects

typedef union
{
    short sVal;
    int iVal;
    long lVal;
    float fVal;
    double dVal;
}DATA;

...
DATA* array = malloc(some_number * sizeof(DATA));
...
scanf("%d",&array[loop].iVal);

yes -- how else can the program pass the correct data type to printf(). There is another solution -- create a union of data types than an array of union objects

typedef union
{
    short sVal;
    int iVal;
    long lVal;
    float fVal;
    double dVal;
}DATA;

...
DATA* array = malloc(some_number * sizeof(DATA));
...
scanf("%d",&array[loop].iVal);

so you are suggesting a Struct of Type to Use and DATA is the field itself?
1= sVal and so on? :-)
we are forced to use VOID* ARRAY ... :-)
anyhow , after i store the data , i need to find the two smallest values.

the prototype is :
void two_min(int(*comp)(void* a, void* b), void **answer, void **array);

and i need to compare functions for each type , 1 Int , 1 Float :

int Compare_Number( void* a , void* b)
{
return *(int*)a - *(int*)b;
}
and the same for float , right?

the requirement is that i need to go over the Data Array only once for each cell in array. is it possible?

>>we are forced to use VOID* ARRAY ... :-)
then my idea of using unions will not work. you will just have to typecast whenever the program accesses the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.