Skip to content

Instantly share code, notes, and snippets.

@speedsticko
Created February 6, 2013 18:05
Show Gist options
  • Save speedsticko/4724510 to your computer and use it in GitHub Desktop.
Save speedsticko/4724510 to your computer and use it in GitHub Desktop.
dereferencing pointer to struct creates copy?
#include <iostream>
using namespace std;
typedef struct Something
{
int number;
} Something, *PSomething;
#define UPDATE_SOMETHING(something, n) (something).number = n;
void modifySomething(PSomething pSomething) {
// 1.
pSomething->number = 99;
// 2.
(*pSomething).number = 99;
/* 3.
Something s = *pSomething;
UPDATE_SOMETHING(s, 9);
cout<<"addr: "<<&s<<endl;
cout<<" .number: "<<s.number<<endl;
*/
}
int main(int argc, char *argv[]) {
Something s1 = {0};
modifySomething(&s1);
cout<<"addr: "<<&s1<<endl;
cout<<" .number: "<<s1.number<<endl;
char line[80];
cin>>line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment