Solved

struct and real type

Posted on 2003-10-29
3
257 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now