pointer problems

/* 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 planet {
string name;
char initial;
double gravity;
int days;
};

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;

return person1;
}

planet planet_Initialize() {

struct planet planet_choice;
char choice;

cout << "Please select a planet(M: Mercury, V: Venus, E: Earth, R: Mars, J: Jupiter, S: Saturn; U: Uranus, N: Neptune): " << endl;
cin >> choice;

switch(choice) {
case 'M':
case 'm': planet_choice.name = "Mercury", planet_choice.gravity = 3.72, planet_choice.days = 88;
break;
case 'V':
case 'v': planet_choice.name = "Venus",planet_choice.gravity = 8.80, planet_choice.days = 225;
break;
case 'E':
case 'e': planet_choice.name = "Earth", planet_choice.gravity = 9.78, planet_choice.days = 365;
break;
case 'R':
case 'r': planet_choice.name = "Mars", planet_choice.gravity = 3.71, planet_choice.days = 690;
break;
case 'J':
case 'j': planet_choice.name = "Jupiter", planet_choice.gravity = 23.1, planet_choice.days = 4332;
break;
case 'S':
case 's': planet_choice.name = "Saturn", planet_choice.gravity = 9, planet_choice.days = 10752;
break;
case 'U':
case 'u': planet_choice.name = "Uranus", planet_choice.gravity = 8.7, planet_choice.days = 30684;
break;
case 'N':
case 'n': planet_choice.name = "Neptune", planet_choice.gravity = 11, planet_choice.days = 60190;
break;
default: cout << "Invalid entry" << endl;

}
return planet_choice;
}

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;

return new_weight;
}
closed account (jwC5fSEw)
I don't know if this has anything to do with the problem, but you need to put in #include <string> if you want to use strings.
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; 
}
Last edited on
Ok. So how can I pass the value of new_age into my main function so I can print it?
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 = new double();

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);

Last edited on
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...
I think you can still do it with one function though...

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;

	return new_weight;
}


That works. Now it doesn't matter that 'new_age' goes out of scope, because the value has been copied to the memory at address pointed to by 'ptr'.
Yeah.. just to add that ptr should no longer be a pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


double ptr;  //notice not a pointer

struct person a = person_Initialize();
struct planet b = planet_Initialize();
double c = formula(a, b, &ptr);
//...

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;

	return new_weight;
}

You could use reference .. if you want ... it looks cleaner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


double ptr;

struct person a = person_Initialize();
struct planet b = planet_Initialize();
double c = formula(a, b, ptr);  //notice here
//...

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;  //and here.. cleaner :)

	return new_weight;
}
Last edited on
Topic archived. No new replies allowed.