指针习题

1. 堆区二维数组练习(无需封装函数)
a) 在堆区申请空间,int a[3][4]
b) 通过循环给每一个元素赋值,以及打印每个元素的值
c) 使用完,需要释放堆区空间
【指针习题】

#define _CRT_SECURE_NO_WARNINGS #include #include #includeint main(void) { //定义一个数组指针类型 typedef int (*PARRAY)[4]; PARRAY p; //定义一个数组指针,步长为int[4] //在堆区申请空间, a[3][4] p = (PARRAY)malloc(sizeof(int)* 3 * 4); if (p == NULL) { perror("malloc"); return -1; } int i, j; int k = 0; //通过循环赋值 for (i = 0; i < 3; ++i) { for (j = 0; j < 4; ++j) { p[i][j] = ++k; } } //通过循环遍历 for (i = 0; i < 3; ++i) { for (j = 0; j < 4; ++j) { printf("p[%d][%d]=%d\t", i, j,*(*(p+i) + j) ); } printf("\n"); } //释放空间 if (p != NULL) { free(p); p = NULL; } printf("\n"); system("pause"); return 0; }









2. 结构体嵌套一级指针和二级指针练习

//结构体类型,每个导师有三个学生
typedef struct Teacher
{
char *tName; //导师名字,需要动态分配空间
char **stu; //三个学生,需要动态分配空间,堆区数组
int age;
}Teacher;

//在createTeacher中分配空间
int createTeacher(Teacher **p/*out*/, int n1, int n2);

//给成员赋值
void initTeacher(Teacher *p, int n1, int n2);

//打印结构体成员信息
void printTeacher(Teacher *p, int n1, int n2);


//根据导师名字排序, 降序
void sortTeacher(Teacher *p, int n);


//释放空间,在函数内部把p赋值为NULL
void freeTeacher(Teacher **p, int n1, int n2);

int main(void)
{
int ret = 0;
int n1 = 3; //导师个数
int n2 = 3; //学生
Teacher *p = NULL;


ret = createTeacher(&p, n1, n2);
if (ret != 0)
{
printf("createTeacher err:%d\n", ret);
return ret;
}


initTeacher(p, n1, n2); //给成员赋值


//打印成员,排序前
printf("排序前:\n");
printTeacher(p, n1, n2);




//根据导师名字排序, 降序
sortTeacher(p, n1);


//打印成员,排序后
printf("\n排序后:\n");
printTeacher(p, n1, n2);


//释放空间,在函数内部把p赋值为NULL
freeTeacher(&p, n1, n2);

return 0;
}



#define_CRT_SECURE_NO_WARNINGS #include #include #include //结构体类型,每个导师有三个学生 typedef struct Teacher { char *tName; //导师 char **stu; //三个学生 int age; }Teacher; //在createTeacher中分配空间 int createTeacher(Teacher **p/*out*/, int n1, int n2) { if (p == NULL) { return -1; } //三个结构体,Teacher t[3] Teacher *t = (Teacher *)malloc(sizeof(Teacher)* n1); if (t == NULL) { return -2; } int i = 0; int j = 0; for (i = 0; i < n1; i++) //每个老师的导师,n2个学生分配空间 { //导师名字 t[i].tName = (char *)malloc(50 * sizeof(char) ); //每个导师有n2个学生 char **tmp = (char **)malloc(n2 * sizeof(char * ) ); //char *tmp[i] for (j = 0; j < n2; j++) { tmp[j] = (char *)malloc(50); }t[i].stu = tmp; //重要 } //间接赋值是指针存在最大意义 *p = t; return 0; }//给成员赋值 void initTeacher(Teacher *p, int n1, int n2) { if (p == NULL) { return; } int i = 0; int j = 0; char buf[50] = { 0 }; for (i = 0; i < n1; i++) { //导师名字 sprintf(buf, "teacher%d%d%d", i, i, i); strcpy(p[i].tName, buf); //每个导师有n2个学生 for (j = 0; j < n2; j++) { sprintf(buf, "stu%d%d%d%d", i, i, j, j); strcpy(p[i].stu[j], buf); }//年龄 p[i].age = 20 + 2*i; }}//打印结构体成员信息 void printTeacher(Teacher *p, int n1, int n2) { if (p == NULL) { return; } //先打印导师,再打印学生 int i = 0; int j = 0; char buf[50] = { 0 }; for (i = 0; i < n1; i++) {printf("%s[%d]\n", p[i].tName, p[i].age); //每个导师有n2个学生 for (j = 0; j < n2; j++) {printf("\t%s", p[i].stu[j]); } printf("\n"); }}//根据导师名字排序, 降序 void sortTeacher(Teacher *p, int n) { if (p == NULL) { return; } int i = 0; int j = 0; Teacher tmp; //选择法排序 for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (strcmp(p[i].tName, p[j].tName) < 0) //降序 { //交换成员变量的值 tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } }}//释放空间,在函数内部把p赋值为NULL void freeTeacher(Teacher **p, int n1, int n2) { if (p == NULL) { return; } Teacher *t = *p; int i = 0; int j = 0; for (i = 0; i < n1; i++) { //先释放导师 if (t[i].tName != NULL) { free(t[i].tName); t[i].tName = NULL; }//再释放导师名下的学生 for (j = 0; j < n2; j++) { if (t[i].stu[j] != NULL) { free(t[i].stu[j]); t[i].stu[j] = NULL; } }if (t[i].stu != NULL) { free(t[i].stu); t[i].stu = NULL; } } if (t != NULL) { free(t); *p = NULL; } }int main(void) { int ret = 0; int n1 = 3; //导师个数 int n2 = 3; //学生 Teacher *p = NULL; ret = createTeacher(&p, n1, n2); if (ret != 0) { printf("createTeacher err:%d\n", ret); return ret; } initTeacher(p, n1, n2); //给成员赋值 //打印成员,排序前 printf("排序前:\n"); printTeacher(p, n1, n2); //根据导师名字排序, 降序 sortTeacher(p, n1); //打印成员,排序后 printf("\n排序后:\n"); printTeacher(p, n1, n2); //释放空间,在函数内部把p赋值为NULL freeTeacher(&p, n1, n2); printf("\n"); system("pause"); return 0; }




3. 画一下第2题createTeacher()的内存模型图
指针习题
文章图片





4. 编写一个函数。其参数为一个字符串,函数删除字符串中的空格

#define _CRT_SECURE_NO_WARNINGS #include #include #include//编写一个函数。其参数为一个字符串,函数删除字符串中的空格。void delspace(char *p1) { char *p2; while (*p1 != '\0') //如果没有到结束符 { if (*p1 == ' ') { p2 = p1; while (*p2 != '\0') { *p2 = *(p2 + 1); p2++; }/*end of while*/p1--; //抵消下面的p1++}/*end of if*/p1++; }/*end of while*/ }int main(void) { char str[] = "abc hello mike "; delspace(str); printf("str = %s\n", str); printf("\n"); system("pause"); return 0; }



    推荐阅读