Results 1 to 11 of 11

Thread: typedef

  1. #1
    Frenzied Member
    Join Date
    Mar 00
    Posts
    1,089
    Hmm, I seem to be the guy asking all the questions on this forum. I apreciate all the answers.

    I seem to remember somewhere that you could use typedef to make a new variable type that could contain one of two variables. so If I made a type OptionalSignedLong that could hold either a signed or unsigned long I could go

    OptionalSignedLong MyNumber = 10;

    MyNumber = (signed long)-10; /* MyNum Stores a
    signed long*/


    MyNumber = (unsigned long) 10000; /* MyNum Stores an
    unsigned long*/


    Is this possible or am i just imaganing things.


  2. #2
    Guest
    You can do it exactly the way you have it, if you type something like

    typedef UnLong unsigned long;

    and the such.

  3. #3
    Frenzied Member
    Join Date
    Mar 00
    Posts
    1,089
    Sorry, I didn't make Myself Clear, I want the same data type to hold both types, Kinda Like A Variant in VB only specific to 2 types. I'm SURE I saw this somewhere.

  4. #4
    Hyperactive Member
    Join Date
    Mar 00
    Posts
    292
    Do you mean an union?
    and I quote:
    "Unions are similar to structures. A union is declared and used in the same ways that a structure is. A union differs from a structure in that only one of its members can be used at a time. The reason for this is simple. All the members of a union occupy the same area of memory. They are laid on top of each other.

    union shared {
    char c;
    int i;
    };

    "People who think they know everything are a great annoyance to those of us who do."

  5. #5
    Frenzied Member
    Join Date
    Mar 00
    Posts
    1,089
    D'OH

    Thanks a lot, It's been staring me in the face but I thought unions were for transmitting data between namespaces or something.

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 02
    Location
    . . . my reason of shame
    Posts
    2,729
    i dont quite get the why of unions even with ur explication..let me see:

    union shared {
    char c;
    int i;
    };

    now if i put

    c = 'A';

    if i understand well the theory if the vars are on the same memory they share the memory right?
    then the i variable will change to 65(A = 65)?
    \m/\m/

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594
    Possibly. But since char is one byte long and int four (usually), the char might occupy either the first or the last byte of the integer, depending on the compiler and the CPU.
    So since 'A' is 41 in hex, the int might be
    00 00 00 41 = 65
    or
    41 00 00 00 = 1,090,519,040
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 02
    Location
    . . . my reason of shame
    Posts
    2,729
    much tks by the answer! actually it was a mistake of my part to think they would have the same values as i forgot that char is like a byte and not like an int

    could u give me an real world example of using an union? are they very used in c++?
    \m/\m/

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594
    Hardly at all. The VB Variant datatype is actually a union in the C/C++ backend.

    There are some tricky float/int conversion optimizations for high-performace/low-accuracy apps (like games) that involve a union like this:
    Code:
    typedef union {
      float f;
      int i;
    } FICONVERT;
    You can read about this in the Game Programming Gems series.

    Then there's the LARGE_INTEGER and ULARGE_INTEGER unions in the Win32 API, which are unions of a 64-bit integer and a struct of two 32-bit integers, to support 64-bit numbers on machines which don't support them. Used to retrieve free disk space for example.

    There were the register unions back in the DOS days. They were used to store the CPU register state, and as CPU registers partially overlap a union is the perfect choice.
    Code:
    struct general_purpose
    {
      union {
        long eax;
        short ax;
        struct {
          char al;
          char ah;
        };
      };
      union {
        long ebx;
        short bx;
        struct {
          char bl;
          char bh;
        };
      };
      union {
        long ecx;
        short cx;
        struct {
          char cl;
          char ch;
        };
      };
      union {
        long edx;
        short dx;
        struct {
          char dl;
          char dh;
        };
      };
    };
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 02
    Location
    . . . my reason of shame
    Posts
    2,729
    so as noob i shouldnt really care about it do i?
    \m/\m/

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 01
    Location
    In a microchip!
    Posts
    11,594
    Exactly
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •