Hi all - so trying to wrap my head around the idea of pointers and addresses. I’m still obviously new to this, but there’s something that’s just not clicking for me.

I ran through the challenge and wrote the code below. I don’t get what the outputs of the pointers and addresses lead to. I thought, and this could be where I’m making my mistake, the idea of pointers and addresses was efficiency. A piece of information is stored at one address, and the pointer sends the function or method to that address to get it, instead of taking up more space to reiterate information that already exists somewhere in the memory.

I wrote the code below with printf functions so I could see what addresses the program was drawing its outputs from. According to these printf’s, the code has different addresses for each iteration of ftPtr and inPtr. I’m trying to understand why.

Am I writing the code inefficiently? I.e. duplicating values in multiple addresses? Am I not understanding how addresses/pointers work? Or maybe are my printf functions not outputting the addresses I think they are?

Thanks for any help on this. I’m really falling in love with the ability to code and I want to make sure I’ve got a strong understanding of this stuff before I move on to tougher challenges.

[code]#include <stdio.h>
#include <math.h>

void metersToFeetIn(double meters, double *ftPtr, double *inPtr) {

``````// Converts meters to raw feet number
double feet = meters * 3.281;

// Split up the rawFeet number
double inches = modf(feet, ftPtr);
*inPtr = inches * 12.0;
printf("Storing %.1f to address %p\n", *ftPtr, &ftPtr);
printf("Storing %.1f to address %p\n \n", inches, &inPtr);
``````

}

// MAIN STARTS HERE

int main(int argc, const char * argv[]) {

``````double meters = 3.0;
double ftPtr;
double inPtr;

metersToFeetIn(meters, &ftPtr, &inPtr);
printf("%.1f meter is equal to %.0f feet and %.1f inches.\n \n", meters, ftPtr, inPtr);
printf("I used the feet value stored at %p and the inches value stored at %p for this calculation.\n", &ftPtr, &inPtr);

return 0;
``````

}[/code]

Hi there

I ran your code and it showed this output:

3.0 meter is equal to 9 feet and 10.1 inches.

I used the feet value stored at 0xbfbb7bf0 and the inches value stored at 0xbfbb7bf8 for this calculation.[/code]

This seems fine to me. First, the variables double ftPtr and double inPtr are located at 0xbfbb7bf0 and 0xbfbb7bf8. When you pass that address to the function, what the compiler does is to create two more variables that point to 0xbfbb7bf0 and 0xbfbb7bf8, respectively (it will put them somewhere, you cannot predict that), and this new variables are stored at different address, as you can see. So when the function is called you will have 4 variables, ftPtr, inPtr, and the ones at the function parameters. When you print:

```Storing 9.0 to address 0xbfbb7bd8 Storing 0.8 to address 0xbfbb7bdc```
You have the address of the new variables, and when you print:

You see the address of ftPtr and inPtr.
This behavior is expected. In C, all parameters passed to a function are made by what is called passing-by-value, so what you did was to pass as a copy (in a different variable->different memory location) the location of the first two variables (the ones in the main).

To add to the previous explanation.

In the above code, because you are printing the values of the expressions &inPtr and &ftPtr, you are effectively printing the addresses of the local variables inPtr and ftPtr.

You need to print the values of the pointers passed to the function from main:

``````void metersToFeetIn(double meters, double *ftPtr, double *inPtr) {

// Converts meters to raw feet number
double feet = meters * 3.281;

// Split up the rawFeet number
double inches = modf(feet, ftPtr);
*inPtr = inches * 12.0;
printf("Storing %.1f to address %p\n", *ftPtr, ftPtr);
printf("Storing %.1f to address %p\n \n", inches, inPtr);
}``````

By the way, the main reason we use pointers is not for efficiency, but for having direct access to an object itself, not to its copy.

I hope this helps.

[quote=“jlhb123”]So when the function is called you will have 4 variables, ftPtr, inPtr, and the ones at the function parameters. When you print:

```Storing 9.0 to address 0xbfbb7bd8 Storing 0.8 to address 0xbfbb7bdc```
You have the address of the new variables, and when you print:

You see the address of ftPtr and inPtr.
This behavior is expected. In C, all parameters passed to a function are made by what is called passing-by-value, so what you did was to pass as a copy (in a different variable->different memory location) the location of the first two variables (the ones in the main).[/quote]

Thanks so much!

[quote=“ibex10”] ``` printf("Storing %.1f to address %p\n", *ftPtr, ftPtr); printf("Storing %.1f to address %p\n \n", inches, inPtr); ```
[/quote]

Yup, thanks! My printf’s weren’t doing what I thought, makes sense now with your example.