I have 2 arrays int and float,one is commented, I would like to check the type of these arrays if int it should send to fun1() otherwise to fun2(): How can I check the type of array?
int arr[]={4,6,7,8};
//float arr[]={5.4,7.4,3.3,};
if (type == int)
fun1(arr);
else if(type == float)
fun2(arr);
else
printf("not");
CodePudding user response:
How can I check the type of array?
You can use generic selection to choose which function to call based on the type of an expression. Example:
#include <stdio.h>
void fun1 (int arr[]) {
puts("My argument is an int pointer");
}
void fun2 (float arr[]) {
puts("My argument is a float pointer");
}
int main() {
// int arr[] = { 1, 2, 3 };
float arr[] = { 1, 2, 3 };
_Generic(arr,
int * : fun1,
float * : fun2)(arr);
return 0;
}
That particular usage doesn't make much sense, as you know what the type is, and, therefore, which function to call. The more usual use case is to use generic selection in a macro, so that it is reusable, and the name of the macro gives you information about what all the type-specific functions do. Something like this:
#define fun(x) _Generic((x), \
int * : fun1, \
float * : fun2)(x)
// ...
fun(arr);
// ...
CodePudding user response:
I think thats what you are looking for...
int arr[]={4,6,7,8};
String type = arr.getClass().getComponentType().toString();
if (type == "int")
fun1(arr);
else if(type == "float")
fun2(arr);
else
printf("not");