Skip to content

Instantly share code, notes, and snippets.

@ramachandrajr
Created May 19, 2017 15:03
Show Gist options
  • Save ramachandrajr/241d35057d8c1b317d36ebcc0b9e390b to your computer and use it in GitHub Desktop.
Save ramachandrajr/241d35057d8c1b317d36ebcc0b9e390b to your computer and use it in GitHub Desktop.
Fixing error with pointer
#include <iostream>
using namespace std;
class Person
{
public:
string name;
int age;
double height;
double weight;
};
void ModifyPerson(Person *pOne);
int main() {
// Person 1
Person person1;
person1.name="Tom";
person1.age=25;
person1.height=175.0;
person1.weight=120.8;
// pOne
Person *pOne = new Person;
*pOne = person1;
cout << person1.name <<endl;
ModifyPerson(pOne);
person1=*pOne;
cout << person1.name <<endl;
return 0;
}
void ModifyPerson(Person *pOne)
{
// Create a person pointer.
Person *person1;
// Put the memory location from pone into person1.
person1 = pOne;
// Manipulate data at that memory location
person1->name = "Tomnew";
cout << "in ModifyPerson():" << person1->name << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment