What is the diffrence (struct with typedef and without)

This is a discussion on What is the diffrence (struct with typedef and without) within the C++ Programming forums, part of the General Programming Boards category; I found this in a tutorial, and they don't explain any difference between these two. I don't get what typedef ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    What is the diffrence (struct with typedef and without)

    I found this in a tutorial, and they don't explain any difference between these two. I don't get what typedef does, since the final outcome is the same (assigning values to vars declared as those structs, and then printing them)

    Code:
    typedef struct _MONEY {			// Create a structure called "_MONEY"
    	int		Dollars;	// We want a variable to hold the Dollars, so we choose an integer.   (ex, 1, 2 333, 1200000) Whole number
    	double	Cents;			// Then we want a variable to hold the Cents, so we choose a double.  (ex, .00003, .02, etc)  Floating point number (decimal number)
    } MONEY;				// Here is our alias, "MONEY".  We now have a variable of our own to use. "MONEY" now means "struct _MONEY"
    
    					// I create two of the same type of structures to show the difference.
    
    struct TEMP {				// Here is just a regular structure, without the typedef.
    	int		Dollars;	// We want a variable to hold the Dollars, so we choose an integer.   (ex, 1, 2 333, 1200000) Whole number
    	double	Cents;			// Then we want a variable to hold the Cents, so we choose a double.  (ex, .00003, .02, etc)  Floating point number (decimal number)
    };

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    In C++, there is no difference (for structures at least), though you can still use typedef for other things.

    MONEY myvar;
    _MONEY myvar;
    TEMP myvar;
    All the same.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    In C++, the typedef is unnecessary, but it is sometimes used to be compatible with C.

    Incidentally, _MONEY, like other identifiers beginning with an underscore followed by an uppercase letter, is reserved to the implementation for any use. You should not use it unless you are writing the compiler or implementing the standard library or something like that.
    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

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    but what happens with the same in C?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    but what happens with the same in C?
    Consider this program:
    Code:
    struct Money
    {
        int dollars;
        double cents;
    };
    
    int main()
    {
        Money money;
    }
    It works in C++, but not C. To make it work in C, you would have to write:
    Code:
    int main()
    {
        struct Money money;
    }
    Having to prefix Money with struct can be tiresome, so the workaround is a typedef:
    Code:
    typedef struct Money
    {
        int dollars;
        double cents;
    } Money;
    
    int main()
    {
        Money money;
    }
    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

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    laserlight, you mean that i should use _MONEY in my file where all the things happen, and MONEY just in the definition (like a header) ? And yes, I know this sounds noobish

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    you mean that i should use _MONEY in my file where all the things happen, and MONEY just in the definition (like a header) ?
    No, do not use _MONEY at all. Just use the "regular" structure definition for C++. There is no need to have a typedef.
    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

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    Oh, I see now. This is odd. Sometimes I wonder why people use C these days.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Quote Originally Posted by _izua_ View Post
    Oh, I see now. This is odd. Sometimes I wonder why people use C these days.
    Ha ha careful... Salem and Quazar will frown at that comment!

    C is the "mother language" of C++ and C# and it's still well used today. It will never die out ( well I dont think it will ) all languages have a usage remember
    I'm just trying to be a better person - My Name Is Earl

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by _izua_ View Post
    Oh, I see now. This is odd. Sometimes I wonder why people use C these days.
    Hamlet: There are more things in heaven and earth, Horatio, Than are dreamt of in your philosophy.

    The whole world's not a desktop PC.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A completely unrelated question regarding the use and purpose of the struct. Is cent's ever going to be "parts of cents?" It seems overly complicated to use the a double, just to store something that could just be a integer between 0 and 99.

    My preferred appraoch (which will also help when you need to perform math on the "Money" struct would be to use a plain integer, and use it as "cents". Any amount can be described as cents:

    $0.01 = 1 cent.
    $11.99 = 1199 cent.
    $4million = 40000000000 cent.

    If you need a range bigger than +/- 20 million, you will need a bigger integer than 32 bits, but most modern compilers have support for 64-bit integers in one way or another.

    To print your amount as a dollar and cent value, just do something like:

    amount = 12399; // $123.99
    cout << "$" << amount / 100 << "." << amount % 100 << endl;


    In C++ you could of course create a class that handles all your money math and input/output with suitable formatting. It would make sense to supply multiply/divide for cent*int and cent*double, as well as "cent + cent" and "cent - cent".

    [Note that when you multiply two "cent" numbers together, you need to divide by 100, (and you need to watch for overflows if you use 32-bit integers). Although, I don't really see a use for multiplying two "cent" values together very often - if at all...

    --
    Mats

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by matsp View Post
    [Note that when you multiply two "cent" numbers together, you need to divide by 100, (and you need to watch for overflows if you use 32-bit integers). Although, I don't really see a use for multiplying two "cent" values together very often - if at all...
    The resulting value would have units of "cents squared," which is not a meaningful monetary unit. So, multiplying money values makes no sense. Dividing them, however, yields a dimensionless ratio, and this can be useful. So at least division should be implemented.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    The resulting value would have units of "cents squared," which is not a meaningful monetary unit. So, multiplying money values makes no sense. Dividing them, however, yields a dimensionless ratio, and this can be useful. So at least division should be implemented.
    Indeed.

    --
    Mats

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    Quote Originally Posted by brewbuck View Post
    Hamlet: There are more things in heaven and earth, Horatio, Than are dreamt of in your philosophy.

    The whole world's not a desktop PC.
    brilliant one
    just that, when i was working with pic microprocessors, which are really low level, its assembly language seemed easier to understand than C. And since C++ has support for strings and the such, that was my question. but eh, i guess there still are limited architectures. Ah well.

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by _izua_ View Post
    brilliant one
    just that, when i was working with pic microprocessors, which are really low level, its assembly language seemed easier to understand than C. And since C++ has support for strings and the such, that was my question. but eh, i guess there still are limited architectures. Ah well.
    It's not so much the limitations of the architecture, as the fact that there is often no C++ compiler available to use. Or, the available C++ compilers cost a fortune, produce terrible code, or whatever.

    I'm sure many C developers would love to develop in C++ instead, they simply can't.

Popular pages Recent additions subscribe to a feed

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