- 论坛徽章:
- 1
|
- #include<iostream>
- using namespace std;
- typedef struct _user
- {
- int id;
- int age;
- }user;
- user *init_user()
- {
- user *ui;
- ui = (user*)calloc(1, sizeof(user));
- return ui;
- }
- void vvv1(user *tmp)
- {
- tmp->id = 2;
- tmp->age = 21;
- }
- void vvv2(user **tmp)
- {
- user *a;
- a = init_user();
- a->id = 4;
- a->age = 30;
- user *b;
- b = *tmp;
- *tmp = a;
- free(b);
- }
- int main()
- {
- user *ui_temp;
- ui_temp = init_user();
- ui_temp->id = 1;
- ui_temp->age = 5;
- cout<<"before, ui_temp->id: "<<ui_temp->id<<"\tui_temp->age: "<<ui_temp->age<<endl;
- vvv1(ui_temp);
- cout<<"after vvv1(), ui_temp->id: "<<ui_temp->id<<"\tui_temp->age: "<<ui_temp->age<<endl;
- vvv2(&ui_temp);
- cout<<"after vvv2(), ui_temp->id: "<<ui_temp->id<<"\tui_temp->age: "<<ui_temp->age<<endl;
- free(ui_temp);
- return 0;
- }
复制代码
- -bash-3.2$ ./ptr
- before, ui_temp->id: 1 ui_temp->age: 5
- after vvv1(), ui_temp->id: 2 ui_temp->age: 21
- after vvv2(), ui_temp->id: 4 ui_temp->age: 30
复制代码
vvv1是传指针,vvv2是传指针的指针。 |
|