function that takes diff data types as paramter

This is a discussion on function that takes diff data types as paramter within the C Programming forums, part of the General Programming Boards category; how do u make a function which takes diff data type as parameter, depending the data type variable pointed..i have ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    function that takes diff data types as paramter

    how do u make a function which takes diff data type as parameter, depending the data type variable pointed..i have understood that the basic function prototype should be like this

    Code:
    void functionname(void *pnum) {
           //do something depending upto datatype
    suppose i have

    Code:
    float fa;
    int ib;
    
    functionname(&fa);
    functionname(&ib);

    this much i have understood...now how do i make the function do according to the datatype???

    i searched the forum and i got that type casting is required? how ??

    thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    You need another parameter to indicate what type the pointer is pointing to, since you can't just look at a void* pointer and decide what it's really pointing at.

    One way is to use printf/scanf style format strings to encode the type information of the rest of the arguments.

    Another way is to use a union of all the possible types, and some kind of identifier to indicate which member of the union is valid.
    Code:
    struct variant {
      enum { isInt, isFloat } type;
      union {
        int anInt;
        float aFloat;
      } v;
    };
    This has the advantage of being reasonably type-safe compared to alternatives.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    One way would be to tell the function what type you're passing to it:
    Code:
    #include <stdio.h>
    
    enum { T_FLOAT, T_INT };
    
    int square(void *data, int type)
    {
      int result;
    
      if(type == T_FLOAT)
        result = (*(float *)data) * (*(float *)data);
      else if(type == T_INT)
       result = (*(int *)data) * (*(int *)data);
      else
       result = 0;
    
      return result;
    }
    
    int main(void)
    {
      int i = 2;
      float f = 3;
    
      printf("&#37;d\n", square(&i, T_INT));
      printf("%d\n", square(&f, T_FLOAT));
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    hey thanks for the replies guys...

    i don't understand the second post at all

    i was always trying to the same as in the third post but i don't get

    [CODE]*(int *)num ==> num is void *[\CODE]

    here we are casting the void num * to int type then deferencing it to the address of stored in the num pointer...and why is it (int *) and not (int)*num?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Because (int *) is the data type you need to cast it to. You have a void *, but you want to treat what it points to as an int, so you cast it to an (int *), and then dereference. You can't dereference a void * at all, and this casting of pointer types is necessary. Period. Don't argue it. C will not allow it.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    hey thanks..not arguing..don't know c that much to do that
    this is for the same function that i am making

    i am taking input from user and depending upon the type, i either int cast or float cast.
    i typecast to int..but suppose the user inputs a value greater than the max limit of int then i get crazy numbers. how can i make sure than if a value greater than INT_MAX than i display an error.

    i tried this
    Code:
     
    
    int i;
    scanf("&#37;d",&i);
    if(i > INT_MAX) 
        printf("ERROR");
    else
        *(int *)ptr = i;
    i did this but still if the value is greater than allowed by int type, it shows some crazy numbers.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Use a data type bigger than an int. Something like a long long.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by nepdude View Post
    hey thanks..not arguing..don't know c that much to do that
    this is for the same function that i am making

    i am taking input from user and depending upon the type, i either int cast or float cast.
    i typecast to int..but suppose the user inputs a value greater than the max limit of int then i get crazy numbers. how can i make sure than if a value greater than INT_MAX than i display an error.

    i tried this
    Code:
     
    
    int i;
    scanf("%d",&i);
    if(i > INT_MAX) 
        printf("ERROR");
    else
        *(int *)ptr = i;
    i did this but still if the value is greater than allowed by int type, it shows some crazy numbers.
    You have to compare the floating point value with INT_MAX before you cast it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM

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