Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ function using float or double or int

Tags:

c++

c

text

io

I have seperate functions for reading from a text file (depending on whether its an int, float or double). I would like just one function with an additional argument (without using a subsequent IF statement). Does anyone have ideas?

Below is the form of my current functions.

float * read_column_f (char * file, int size_of_col){
...
col = (float*) malloc (height_row * sizeof(float));
...  return(col);}


double *    read_column_d (char * file, int size_of_col){
...
col = (double*) malloc (height_row * sizeof(double));
...  return(col);}


int *   read_column_i (char * file, int size_of_col){
...
col = (int*) malloc (height_row * sizeof(int));
...  return(col);}

EDIT: I want to implement this in C++, the C-style syntax used is due to memory preference.

like image 361
swarm999 Avatar asked Dec 20 '10 16:12

swarm999


1 Answers

ANSI C doesn't support function overloading, which is what you are trying to accomplish. C++ does, however. See the StackOverflow link here: Default values on arguments in C functions and function overloading in C

like image 133
Nathan Garabedian Avatar answered Sep 30 '22 06:09

Nathan Garabedian