GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Dec-2003, 07:55
mediaware2002 mediaware2002 is offline
New Member
 
Join Date: Dec 2003
Posts: 1
mediaware2002 is on a distinguished road
Question

'stuct' instead of just 'int'


Recently I had following test question:

Why would one ever write:
struct S { int x; } instead of just “int” ?

E.g.
“void Foo(S);” or “S Bar(S);”

instead of:

“void Foo(int);” or “int Bar(int)”

Do you have some ideas?
Thanks.
  #2  
Old 08-Jan-2004, 10:33
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Doing this can increase code readability. Say you've a program that does arithmetic on vectors and matrices. Being a clever coder, you use dynamically allocated memory, and use pointers to access. But a pointer to a vector will look identical to a matrix. A function multiplying 2 matrices gives a matrix, but matrix by a vector gives a vector.

So the code would be like:
CPP / C++ / C Code:
double* mult(double*, double*); //matrix
double* mult(double*, double*); //vector
Pretty hard to read this, eh? But use structs, and things become a lot easier to read:
CPP / C++ / C Code:
struct vector {double* root; }
struct matrix {double* root }

matrix mult(matrix, matrix);
vector mult(matrix, vector);
A lot easier to comprehend! (Not note, C doesn't allow function overloading)

GF
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.