# What is the diffrence (struct with typedef and without)

• 08-09-2007
_izua_
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) };```
• 08-09-2007
Salem
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.
• 08-09-2007
laserlight
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.
• 08-09-2007
_izua_
but what happens with the same in C?
• 08-09-2007
laserlight
Quote:

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; }```
• 08-09-2007
_izua_
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 :)
• 08-09-2007
laserlight
Quote:

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.
• 08-09-2007
_izua_
Oh, I see now. This is odd. Sometimes I wonder why people use C these days.
• 08-09-2007
swgh
Quote:

Originally Posted by _izua_
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
• 08-09-2007
brewbuck
Quote:

Originally Posted by _izua_
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.
• 08-09-2007
matsp
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
• 08-09-2007
brewbuck
Quote:

Originally Posted by matsp
[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.
• 08-09-2007
matsp
Quote:

Originally Posted by brewbuck
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
• 08-09-2007
_izua_
Quote:

Originally Posted by brewbuck
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.
• 08-09-2007
brewbuck
Quote:

Originally Posted by _izua_
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.