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]