结构体相关操作(内存申请、赋值、访存)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#include <string.h>
struct student
{
int age;
char name[10];
};
int main()
{
struct student st1;
struct student *st2,*tmp;
struct student **st3;
struct student *st4[3];
printf("st1:%p st2:%p st3:%p st4:%p st4[0]:%p st4[1]:%p st4[2]:%p \n",st1,st2,st3,st4,st4[0],st4[1],st4[2]);
/****************************************申请内存*************************/
st2 = (struct student *)malloc(sizeof(struct student));tmp = (struct student *)malloc(sizeof(struct student));
st3 = &tmp;
st4[0] = (struct student *)malloc(sizeof(struct student));
st4[1] = (struct student *)malloc(sizeof(struct student));
st4[2] = (struct student *)malloc(sizeof(struct student));
/**************************************************************************/
st1.age = 10;
(*st2).age = 11;
(*(*st3)).age = 12;
(*(*st4)).age = 13;
(*(*(st4 + 1))).age = 14;
(*(*(st4 + 2))).age = 15;
strcpy(st1.name,"sun");
strcpy((*st2).name,"jin");
strcpy((*(*st3)).name,"lei");
strcpy((*(*st4)).name,"zhang");
strcpy((*(*(st4 + 1))).name,"na");
strcpy((*(*(st4 + 2))).name,"ca");
/************************************************************************/
printf("st1:%d st2:%d st3:%d st4[0]:%d st4[1]:%d st4[2]:%d\n",st1.age,(*st2).age,(**st3).age,(*(*st4)).age,(*(*(st4 + 1 ))).age,(*(*(st4 + 2 ))).age);
printf("st1:%s st2:%s st3:%s st4[0]:%s st4[1]:%s st4[2]:%s\n",st1.name,(*st2).name,(**st3).name,(*(*st4)).name,(*(*(st4 + 1 ))).name,(*(*(st4 + 2 ))).name);
}