结构体
-
typedef struct _room
-
{
-
int id;
-
char name[32];
-
int *p;
-
int phone;
-
}room;
-
-
room g_room = { 1, "room", NULL, 123};
//指针取值
-
void get_roomp(room **r)
-
{
-
*r = &g_room;
-
}
-
//变量取值
-
void get_room(room *r)
-
{
-
memcpy(r, &g_room, sizeof(room));
-
}
-
void test_struct()
-
{
-
printf("%p, id = %d,%p, name = %s, %p, %p, phone = %d, %p\n",
-
&g_room,
-
g_room.id,
-
&g_room.id,
-
g_room.name,
-
&g_room.name,
-
&g_room.p,
-
g_room.phone,
-
&g_room.phone);
-
-
room *r;
-
-
get_roomp(&r);
-
printf("%p, id = %d,%p, name = %s, %p, %p, phone = %d, %p\n",
-
r,
-
r->id,
-
&r->id,
-
r->name,
-
&r->name,
-
&r->p,
-
r->phone,
-
&r->phone);
-
-
room rom;
-
get_room(&rom);
-
printf("%p, id = %d,%p, name = %s, %p, %p, phone = %d, %p\n",
-
&rom,
-
rom.id,
-
&rom.id,
-
rom.name,
-
&rom.name,
-
&rom.p,
-
rom.phone,
-
&rom.phone);
-
}
简单变量
-
int g_val = 1;
-
-
//变量取值
-
void get_val(int *v)
-
{
-
*v = g_val;
-
printf("%p,%p\n", v, &v);
-
}
-
-
//指针取值
-
void get_valp(int **v)
-
{
-
*v = &g_val;
-
}
-
void test_val()
-
{
-
printf("%p\n", &g_val);
-
/*addr|val
-
* | |
-
* --|--|
-
* &v|v |
-
* --|--|
-
* | |
-
* --|--|
-
* | |
-
* --|--|
-
*/
-
int v = 0;
-
printf("%p\n", &v);
-
get_val(&v);
-
printf("%p, %d\n", &v, v);
-
/*addr|val |val
-
* | | | |
-
* --|--| |--|
-
* &p|p | |*p|
-
* --|--| |--|
-
* | | | |
-
* --|--| |--|
-
* | | | |
-
* --|--| |--|
-
*/
-
int *p;
-
printf("%p\n", p);
-
get_valp(&p);
-
printf("%p, %d\n", p, *p);
-
}
指针取值
通过修改指针的指向的地址(*p),不需要分配内存,
*v = &g_val;
-
void get_valp(int **v)
-
{
-
*v = &g_val;
-
}
通过函数获取需要传递二级指针
(room **r)
-
void get_roomp(room **r)
-
{
-
*r = &g_room;
-
}
变量取值
1. memcpy
-
void get_room(room *r)
-
{
-
memcpy(r, &g_room, sizeof(room));
-
}
2. 一个个赋值,比较慢
-
void get_val(int *v)
-
{
-
*v = g_val;
-
printf("%p,%p\n", v, &v);
-
}
阅读(671) | 评论(0) | 转发(0) |