Solved

struct and real type

Posted on 2003-10-29
3
263 Views
Last Modified: 2010-04-15

typedef struct buzz {
int a;
int b;
} strtype;

...........
...........

strtype num={142,2804};
// a=142
// b=2804
// c=142.2804
how can i convert to a and b ande merge them c;
with using a function.


 
0
Comment
Question by:xinex
3 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 80 total points
ID: 9641700
In C and C++, the fields of a structure are referenced via the . operator, e.g.


     srtype num = {142, 2804};
     int a = num.a;
     int b = num.b;
     int sumAB  = a + b;   /* or = num.a + num. b */

In this case, you haven't defined what you mean by "merge."  It appears that what you want is:

     double c = a + (0.0001 * b);

but even this isn't quite right, because it hardwired the multiplier.  Chances are you really want to calculate this based on the size of num.b.  To do this, you need to calculate the number of digits in num.b (easy to do using the floor and log10 functions, or manually by dividing by 10.0 until the result is less than 1.0 and counting the number of iterations), and then divide num.b by 10.0 to that number plus 1.  The tricky part is keeping track of which calculations need to be floating point and which need to be integral.   I'm omitting the actual formula because this sounds too much like homework.

As for doing it via a function, functions can take structs as parameters and return doubles or float as results.  In this case, you can choose to pass the struct by value (because it's small enough not to matter) or by address.  If you pass it by address, then there's a syntactic shortcut for referring to the fields, using p->fieldname instead of having to use (*p).fieldname.  For example,

     double someFunction(strtype* pSomeStruct) {
        int a  = someStruct->a;
        ....
     }

Gary
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question