Passing any array type to a function in C -
i trying write function makes ordenations on numeric array given it. problem c needs know data type iterate through array properly, because of "pointer nature". there way can pass array without knowing type? maybe using void type?
void ordenation (void *array, ...) {}
here's how understand question, please reply below if that's not it. trying create function in c can accept arrays of multiple numeric types (float, int, etc.).
this called function overloading. possible in c11 via _generic operator. see function overloading in c
another option create functions different name, different parameter list. so:
void ordenation_i(int *array); void ordenation_f(float *array);
another way using void* type. mean can pass pointer , may not looking for. have cast void* pointer else, float. if operation fails, may have problems.
void ordenation(void *array){ float* array_casted = (float*) array; // stuff }
the problem in code array can used float array.
finally, use c++ compiler. should compile c code , function overloading default.
void ordenation(int array[]){ //do stuff } void ordenation(float array[]){ //do stuff }
Comments
Post a Comment