Information about unclear in example

Hi,

As I recall there were no info previous in book, maybe I have to just take it as it is and dont ask questions, but still.

  1. I see that in last two examples in book appeared new value types for placeholders , with & in the begining. like here:
    metersToInches(meters, &feet, &inches);

What are these? Why is & needed?

  1. Looking at examples, and dont understand, when to declare a double or int. Why should I say that rawFeet is double? Why cannot it be int ?
    double rawFeet = meters * 3.281; 
  1. Cannot understand what this functon means.
    unsigned int feet = (unsigned int)floor(rawFeet);

Go back one chapter. You’re looking at the Pass By Reference chapter, yes? The & operator is discussed in the Addresses and Pointers chapter.

The short version:

  • is the indirection operator - using it on a pointer will cause your statement to operate on the value the pointer points to rather than the value of the pointer itself (which is a numerical memory address).
    & is the “address of” operator - using it on a pointer will cause your statement to operate on the numerical address of the pointer itself rather than its value (which is the numerical memory address address of something else).

Note that both of these operators have different meanings for math and boolean expressions, but these are their meanings when applied to pointers.

But definitely have another look at the Addresses and Pointers chapter.

Thank you - I will do it. (doing it now :slight_smile: )

Could you please tell about the 2. point. Why there is double ?

One more questions, so to make sure I am clear about everything.

metersToFeetAndInches(meters, &feet, &inches);

In this line of code, means that we are passing in to the function value of meters and passing in an adress to feet and inches which are located in Main function. Did I get it correct ?

You nailed it! That’s exactly right.

Also, the reason that we’re using a double to store rawFeet is that we don’t want the decimal part of the number to be truncated off just yet. If you’d captured it into an int, then there wouldn’t be a decimal part to use in order to figure out how many inches there are.

You’re on the right track, though, that we eventually want feet to be an int, but one of the steps in getting there is having a number that we can use with modf() to extract the separate integer and decimal pieces for creating the feet and inches, respectively.

Does that help?

Yes, that does help!

As without any background in programming, it is hard to understand, but your guide, until now is the best i have seen! hope that I will not nees anu else :slight_smile:

Last questions - why not use float insteas of double?

When deciding between float and double, it’s important to think about how big the numbers are that you will be dealing with, and how much you care about having lots of digits of precision. A double is just like a float, only it takes up a little more memory and as such can hold bigger numbers.

It’s also important to look at the functions that you’re using. The modf() function is declared as expecting doubles as arguments and has a return type of double. Using a float variable to capture the double return value runs the risk of trying to put a number in the variable that is too big for a float to hold, which can have bad consequences for your application.

As a thought experiment, though, consider going the other way. Consider using a different function that you know will return a float, but you declare your variable as a double. In this case, you’ll be fine, although a purist might accuse you of being wasteful, since your variable can hold values that take up twice as much memory as the largest number that the function you’re calling will ever return. It’d be like putting a chair in a box meant for a couch.

Peeeerfect! Everything is clear!

Thank you