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 :-)

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.