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 :-)
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 :-)
#include<stdio.h>
#include<stdlib.h>
// void two_min(int(*comp)(void* a, void* b), void **answer, void **array);
int main()
{
int size_of_input=0;
int choise,loop;
void* user_array;
printf("Please Enter Type of Array to use : 1=Integer 2=Float");
scanf("%d",&choise);
switch (choise)
{
case 1 : { (*int)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",&user_array[loop]); }
}
case 2 : { user_array=(*float)malloc(size_of_input*(sizeof(float)));
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("%f",&user_array[loop]); }
}
default : { printf("No such choise can be made , Exiting"); exit(1); break;}
}
return 0;
} 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):
const char *base = array;cur = base + i*elem_size; compare with min1 and min2 using cmp.out_min1/out_min2 (or return indices/pointers if you prefer).Tips and cautions:
void** array (an array of pointers) iterate with void *elem = ((void**)array)[i] instead of byte arithmetic.count <= SIZE_MAX / elem_size before multiplying.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.
Jump to Post— Ancient Dragon 5,243a 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 …
Jump to Post— Ancient Dragon 5,243yes -- 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* …
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.