Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

struct and real type

Posted on 2003-10-29
3
Medium Priority
?
270 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

610 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