Variable function argument types

This is a discussion on Variable function argument types within the C Programming forums, part of the General Programming Boards category; I was wondering if one could write a function that could accept one or the other variable type. ex: I ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Variable function argument types

    I was wondering if one could write a function that could accept one or the other variable type.

    ex: I have 2 arrays, int** and double**, and a function
    Code:
    void PGMWrite(double** Matrix, int Matrix_dimension){.....}
    is there any way to change the function to
    Code:
    void PGMWrite(int** Matrix || double** Matrix, int Matrix_dimension){.....}
    and then have some sort of type identifier in the function that picks the correct section via an if loop? If so how, and how would I identify in the function if the input it type double or int?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    What does PGMWrite do? Show us its implementation with say an int** first parameter.

    One approach might be to use a pointer to void then cast to the appropriate type based on say, an enum. Or, you might just have two different functions. Or you might have two parameters, then pass a null pointer to leave the parameter unused. Or, a macro might work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,255
    Quote Originally Posted by laserlight View Post
    Or, you might just have two different functions.
    This is a common solution for this problem. The function with the fewer number of parameters calls the other function with more parameters, passing reasonable default parameters for the ones not input by the function with the fewer number of parameters. The function that takes a larger number of parameters could have some if statements to recognize that it was called by the other function. This is sometimes called a helper function.

    C++ Helper Functions | Talk Binary
    Last edited by rcgldr; 06-12-2013 at 09:49 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,145
    O_o

    You can't generally get parametric polymorphisms in portable, standard C; you can do some stuff with macros if your compiler has some flavor of `typeof', but that's abnormal, and I don't recommend it.

    I'd recommend using inheritance polymorphisms, the C flavor, if you want to use the same function name.

    Soma

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you all,

    Having 2 different functions, one for int** and one for double** is a but bulky as the function would be exactly the same simply with a cast. So I think that rcgldr's follow up on laserlight's comment is a good one namely creating a function that takes either
    int** or double** with a null and then calls the approperiate cast for PGMwrite() or whatever the function might be.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Argument inside the Argument of a Function Declaration
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2011, 05:53 AM
  2. variable value not assignment to function argument
    By Almsoo7 in forum C Programming
    Replies: 2
    Last Post: 02-28-2010, 11:06 AM
  3. Command line argument types
    By radnik in forum C++ Programming
    Replies: 10
    Last Post: 06-30-2008, 04:58 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. What argument for a function? for static array variable
    By liechmaster in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 04:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21