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

Attachments
#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;
}

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.

This article has been dead for over six months. Start a new discussion instead.