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
Solved

struct and real type

Posted on 2003-10-29
3
262 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coverting 24 hour time to 12 hour in C++ 15 178
Linux context switch  - loop takes long time to process 6 128
Unable to start eclipse ? 17 152
delete-remove 14 97
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

791 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