## 问题一

### version 1

```/*
Name: 二级指针
Copyright: 52coder.net
Author: 52coder
Date: 03/06/17 17:24
Description: pointer
*/

#include <stdio.h>
#include <stdlib.h>

/*获取malloc内存的地址*/
int getaddress(int *address)
{
int size = 64;
address = malloc(size *sizeof(int));
printf("Address of malloc memory is:%p\n",address);
return 0;
}
int main()
{
int *pointer = NULL;
getaddress(pointer);
printf("Address of pointer is:%p\n",pointer);
return 0;
}
```

Address of malloc memory is:0x100500270
Address of pointer is:0x0
Program ended with exit code: 0

pointer 的值居然是0x0，换言之我们没有将所申请内存的地址带出来.

### version 2

```/*
Name: 二级指针
Copyright: 52coder.net
Author: 52coder
Date: 03/06/17 17:24
Description: pointer
*/

#include <stdio.h>
#include <stdlib.h>

/*获取malloc内存的地址*/
int getaddress(int **address)
{
int * tmp = NULL;
int size = 64;
tmp = malloc(size *sizeof(int));
*address = tmp;
printf("Address of malloc memory is:%p\n",tmp);
return 0;
}
int main()
{
int *pointer = NULL;
getaddress(&pointer);
printf("Address of pointer is:%p\n",pointer);
return 0;
}
```

Address of malloc memory is:0x100202350
Address of pointer is:0x100202350
Program ended with exit code: 0

pointer的值就是我们在函数中所申请的内存地址.

### version 3

```/*
Name: 二级指针
Copyright: 52coder.net
Author: 52coder
Date: 03/06/17 17:24
Description: pointer
*/

#include <stdio.h>
#include <stdlib.h>

/*获取malloc内存的地址*/
int getaddress(int **address)
{
int size = 64;
*address = malloc(size *sizeof(int));
printf("Address of malloc memory is:%p\n",address);
return 0;
}
int main()
{
int *pointer = NULL;
getaddress(&pointer);
printf("Address of pointer is:%p\n",pointer);
return 0;
}
```

Address of malloc memory is:0x7fff5fbff7b0
Address of pointer is:0x100300000
Program ended with exit code: 0

## 问题二

```/*
Name: 二级指针
Copyright: 52coder.net
Author: 52coder
Date: 03/06/17 17:24
Description: pointer
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
int age;
int height;
}stu;

typedef struct school
{
int num;
stu **boys;
}grade;

int main()
{
int i = 0;
stu *tmp = NULL;
grade oneclass;

//定义班级男生个数
oneclass.num = 55;

grade *one = &oneclass;

//申请内存
tmp = malloc(oneclass.num * sizeof(stu));

one->boys = &tmp;

//打印三种操作方法对应的地址
for (i = 0; i < 55; i++)
{
printf("tmp + i =%p &tmp[i] = %p *one->boys+i =%p\n",
tmp+i, &tmp[i], *(one->boys)+i);
}

//设置55个元素的age
for (i=0; i<55; i++)
{
tmp[i].age = i;
}
//设置55个元素的height
for (i=0; i<55; i++)
{
tmp[i].height = 55-i;
}

for (i=0; i<55; i++)
{
printf("%d ", (*(one->boys))[i].age);
}

for (i=0; i<55; i++)
{
printf("%d ", (*one->boys)[i].height);
}

return 0;
}
```

```/*
Name: 二级指针
Copyright: 52coder.net
Author: 52coder
Date: 03/06/17 17:24
Description: pointer
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
int age;
int height;
}stu;

typedef struct school
{
int num;
stu **boys;
}grade;

int main()
{
int i = 0;
stu *tmp = NULL;
grade oneclass;

//定义班级男生个数
oneclass.num = 55;

grade *one = &oneclass;

//申请内存
tmp = malloc(oneclass.num * sizeof(stu));

one->boys = &tmp;

printf("\n");

//验证地址与值的关系
printf("&tmp = %p,one->boys = %p\n",&tmp,one->boys);
printf("tmp = %p,*(one->boys) = %p\n",tmp,*(one->boys));
printf("tmp + 10 = %p,*(one->boys) + 10 = %p\n",tmp+10,*(one->boys)+10);

return 0;
}
```