/* Im using a cout statement to try and print the value stored in the address that my pointer is pointing to, but its not working. It compiles just fine but when I run the code it loads up some GDB program in xcode. I would really appreciate some help cause im totally at my wits end. thanks!
heres the code... */
#include <iostream>
using namespace std;
struct person person_Initialize();
struct planet planet_Initialize();
double formula(person a, planet b, double *ptr);
struct person {
string name;
int age;
int weight;
};
int main() {
double *ptr;
struct person a = person_Initialize();
struct planet b = planet_Initialize();
double c = formula(a, b, ptr);
cout << a.name << " you weight is " << a.weight << " kgs and are " << a.age << " years old on Earth, on " << b.name <<", your weight is "<< c << " and you are " << *ptr << " years old." << endl;
return 0;
}
person person_Initialize() {
struct person person1;
cout << "Enter your name: " << endl;
cin >> person1.name;
cout << "Please enter your age: " << endl;
cin >> person1.age;
cout << "Please enter your weight (in kgs) " << endl;
cin >> person1.weight;
new_age goes out of scope when the function returns.. so the pointer will point to a strange place in memory.
Also to be able to modify what a pointer points to... in a function call you would need a pointer to a pointer... should look like this:
Also this will probably compile.. and probably work as you would expect .. but i still wouldn't be correct.. because the variable that ptr points to is out of scope.
1 2 3 4 5 6 7 8 9 10
double formula(person a, planet b, double **ptr) {
double new_weight, new_age;
new_weight =(a.weight/9.78) * b.gravity;
new_age = (a.age * 365) / b.days;
*ptr = &new_age; //this changes what the pointer that has been send as a parameters points to..
//still new_age will go out of scope when the function returns
return new_weight;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
double formula(person a, planet b, double *ptr) {
/* *ptr is a local pointer... so if you change what it points to..
it will not change what the pointer in the main function points too .. you need a **ptr for that
double new_weight, new_age; */
new_weight =(a.weight/9.78) * b.gravity;
new_age = (a.age * 365) / b.days;
ptr = &new_age; //new_age goes out of scope when the function returns..
//but then again .. so will ptr, and the ptr in the main
//function will not change.. and you're dereferencing a
//uninitialized pointer.
return new_weight;
}
So if one compiles this in Visual Studio one receives the C4700 warning, talking about how your 'ptr' variable is being used in your call to 'formula' above before it is initialised. Now if one ignores this warning and attempts to run the program in debug mode, one receives a run-time error at the call to 'formula', i.e.:
double c = formula(a, b, ptr);
Interestingly, if one runs the program in release mode, the program functions without an issue (which I think is what's supposed to happen, right?). Anyway, you could try the following in your declaration of 'ptr':
double * ptr = newdouble();
This will dynamically allocate some memory for a double and set the value of the pointer. I don't think there's any issue with doing that. Just make sure you clean up the memory with 'delete' after you've finished using it.
You could make an additional function ... instead of double formula(person a, planet b, double *ptr) you could have two functions to return what you need.
1 2
double getWeight(person a, planet b);
double getAge(person a, planet b);
Hmmm...I was a bit late :). I think you should follow what adikid89's talking about...I didn't actually have a look to see what was going on in your 'formula' function...