?
Solved

struct and real type

Posted on 2003-10-29
3
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 320 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

Technology Partners: 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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 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