type swapping?

This is a discussion on type swapping? within the C Programming forums, part of the General Programming Boards category; Okay, I'm writing a function where it's going to have to deal with three possible data types in an identical ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    53

    type swapping?

    Okay, I'm writing a function where it's going to have to deal with three possible data types in an identical manner...

    currently, I'll have to do the following
    Code:
       void *arrayPtr;
       if(datatype == 1)
       {
            arrayPtr = calloc(5, sizeof(int));
            int holder;
        }
        else if(datatype == 2)
        {
            arrayPtr = calloc(5, sizeof(double));
            double holder;
        }
        else if(datatype == 3)
        {
            arrayPtr = calloc(5, sizeof(float));
            float holder;
        }
    now, is it possible to create a function or macro that can let me replace the top with something like this?

    Code:
    void *arrayPtr;
    arrayPtr = calloc(5, sizeof(TYPECONV(datatype)));
    TYPECONV(datatype) holder;

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Code:
    #define TYPECONV(t)\
    (t==1?sizeof(int):\
     t==2?sizeof(double):\
     t==3?sizeof(float):\
     sizeof(char))
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by Sebastiani
    Code:
    #define TYPECONV(t)\
    (t==1?sizeof(int):\
     t==2?sizeof(double):\
     t==3?sizeof(float):\
     sizeof(char))
    This works for the sizeof part, but the tricky part is this:
    Code:
    TYPECONV(t) newvariable //<---- Declaring a new variable in the scope with the same datatype...
    Is this possible at all in C, or should I stop barking up the wrong tree?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Sounds to me like you need to use a union
    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.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Other than the part where I can't find enough information to determine if this works, unions sounds just like what I need...

    Code:
    #define TYPESWAP(x)\
    (t==1?swapvar.intvalue\
     t==2?swapvar.floatvalue:\
     swapvar.doublevalue)
    
    
    union vartype
    {
    
    int intvalue; float floatvalue; double doublevalue;
    }swapvar;
    will sizeof(swapvar.intvalue) give me the size of an int variable?
    what about union vartype *swapPtr?
    is can *swapPtr be used as a pointer to a variable with the datatype of one of the members?

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Nevermind, tried it out myself, and the only thing I ever got was errors...
    When I compile, I get this error:

    20 C:\Dev-Cpp\prototyping\typeconv.c invalid lvalue in unary `&'

    Posting code...
    Any ideas how to make this work?

    Code:
    #define TYPESWAP(t)\
    (t==1?swapvar.intvalue:\
     t==2?swapvar.floatvalue:\
     swapvar.doublevalue)
    
    
    union vartype
    {
    
    int intvalue; float floatvalue; double doublevalue;
    }swapvar; int main(int argc, char *argv[]) {
    printf("inserting test int: "); scanf("%d", &TYPESWAP(1)); printf("%d\n", TYPESWAP(1)); system("PAUSE"); return 0;
    }

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    change the scanf call to:
    Code:
    scanf("%d", (int *)&swapvar);
    Edit: It appears it doesn't like what the conditional statement that the macro expands to inside scanf.

    Inside of printf you will need to cast it to the right type if its not a double. ie:
    Code:
    printf("%d", (int)TYPESWAP(1));
    Last edited by Thantos; 03-31-2004 at 08:10 AM. Reason: Add Even More

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Which actually defeats the reason I wanted to try that out in the firstplace, so nevermind.
    Thanks anyway...

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    Post

    Don't ever say nevermind there is always a way
    Code:
    typedef union vartype{
    int intvalue;
    float floatvalue;
    double doublevalue;
    } vartype;
    now
    Code:
    var=vartype{1}; /*this is an int*/
    I think this would work. But really the union is sizeof largest element. Will this work(no compiler on me!)

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think this would work.
    Why?

    >Which actually defeats the reason I wanted to try that out in the firstplace, so nevermind.
    Using scanf and printf in a type independent way is virtually impossible. Even where you don't need a cast, you still need to match the format modifier to the desired type.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    >Which actually defeats the reason I wanted to try that out in the firstplace, so nevermind.
    Using scanf and printf in a type independent way is virtually impossible. Even where you don't need a cast, you still need to match the format modifier to the desired type.
    actually that was a program to test and see if I can make the program automatically treat the union as one of the members without having to specifically calling the member.

    essentially, what I was looking for, was either a way to make the union call the apropriate member by doing something like this:

    myownfunction( SELFCONVERTINGUNION(datatype));

    as opposed to having to do this
    Code:
    switch(datatype)
    {
    
    case 1:
    myownfunction(unionvar.member1); break;
    case 2:
    myownfunction(unionvar.member2); break;
    . . .
    }

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    /whistle
    Code:
      #define INPUT(x) \
      ( x == 1 ? scanf("%d", &unionvar) : \
      	x == 2 ? scanf("%f", &unionvar.floatvalue) :\
     	scanf("%f", &unionvar.doublevalue) \
      )
       
       #define OUTPUT(x) \
      ( x == 1 ? printf("%d\n", unionvar) : \
       	x == 2 ? printf("%f\n", unionvar.floatvalue) :\
       	printf("%f\n", unionvar.doublevalue) \
      )
       
       #include <stdio.h>
       
       int main(void)
       {
       union
       {
     	int intvalue;
     	float floatvalue;
     	double doublevalue;
       }unionvar;
       
        INPUT(1);
       OUTPUT(1);
     
        INPUT(2);
       OUTPUT(2);
     
        putchar('\n');
     
        return 0;
       }
    Sorry for spacing. Not at home and using a different editor and browser
    Last edited by Thantos; 04-01-2004 at 03:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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