C 结构体嵌套一级指针 二级指针 动态分配内存

https://blog.csdn.net/xielinhua88/article/details/51364623

点击打开链接
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//结构体嵌套一级指针 二级指针 动态分配内存
typedef struct _Teacher {
int age;
int id;
char *title;
char **pStuarray;
char name[64];
}Teacher;
//打印结构体
void printfTeacher(Teacher *array,int num) {
int i = 0;
for ( i = 0; i < num; i++)
{
printf("%d\n", array[i].age);
printf("%s\n",array[i].pStuarray[i]);
}
}
//给结构体分配内存
Teacher* createTarray(int num) {
int i = 0,j=0;
Teacher* teacher;
teacher = (Teacher*)malloc(sizeof(Teacher)*num);
if (teacher!=NULL)
{
for ( i = 0; i < num; i++)
{
teacher[i].title = (char*)malloc(sizeof(char)*100);//结构体Teacher title分配内存
char** ptmp = (char**)malloc(sizeof(char*)*num);//
for ( j = 0; j < num; j++)
{
ptmp[i] = (char*)malloc(100);//
}
teacher[i].pStuarray = ptmp;
}
}
return teacher;
}
//释放内存
void freeTeacher(Teacher* arrayT,int num) {
int i = 0,j=0;
if (arrayT!=NULL)
{
for (i = 0; i < num;i++) {
if (arrayT[i].title != NULL) {
free(arrayT[i].title);
}
if (arrayT[i].pStuarray!=NULL)
{
for (j = 0; j < num; j++)
{
free(arrayT[i].pStuarray[j]);
}
}
free(arrayT[i].pStuarray);
}
free(arrayT);
}
}
void main() {
int i = 0,j=0;
Teacher *pArray=NULL;
int num = 1;
pArray = createTarray(num);
for (i = 0; i < num; i++)
{
printf(" %d---age?\n", i + 1);
scanf("%d", &pArray[i].age);
printf(" %d---title?\n", i + 1);
scanf("%s", pArray[i].title);
printf(" %d---name?\n", i + 1);
scanf("%s", pArray[i].name);
for (j = 0; j < num; j++)
{
printf(" ---student----%d\n",i+1);
scanf("%s", pArray[i].pStuarray[j]);
}
}
printfTeacher(pArray,num);
freeTeacher(pArray, num);
system("pause");
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384027.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

线程的同步与互斥

1. 互斥量 在Linux 下线程使用的都是局部变量, 而我们知道, 每一个线程都独立拥有自己的一个栈, 而这些局部便令就在栈中,而线程的创建就是为了实现通信, 此时线程之间无法共享这些变量     为了使得线程之间能够共享数据, 一次我们可以创建一个全局变量, 此时线程都在进程…

二级指针与指针数组的关系

http://blog.csdn.net/shuaishuai80/article/details/6129742 #include <stdio.h> void test(char *argv[]); int main(void) { char *argv[3]{{"abcdefg"},{"1234567"},{"q1w2e3r"}}; test(argv); /*调用指针数组…

UVa1584

【题目描述】 传送门 【题目分析】 也是一道简单的模拟题&#xff0c;1A嘿嘿嘿。 再看书发现和书上的做法差不多。 【AC代码】 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstd…

cf#582div3 D——暴力

【题目描述】 The only difference between easy and hard versions is the number of elements in the array.You are given an array a consisting of n integers. In one move you can choose any ai and divide it by 2 rounding down (in other words, in one move you c…

C语言 可变参数

http://www.cnblogs.com/zhanggaofeng/p/6434554.html //可变参数 #include <stdio.h> #include <stdlib.h> #include <string.h> //引用头文件 #include <stdarg.h>/* va_list用于声明一个变量&#xff0c;我们知道函数的可变参数列表其实就是一个字符…

UVa1585

【题目描述】 传送门 【题目分析】 氵 【AC代码】 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstdlib> #include<set> #include<map> #include<vector>u…

c语言经典算法——查找一个整数数组中第二大数

https://www.cnblogs.com/dootoo/p/4473958.html 题目&#xff1a; 实现一个函数&#xff0c;查找一个整数数组中第二大数。 算法思想&#xff1a; 设置两个变量max1和max2&#xff0c;用来保存最大数和第二大数&#xff0c;然后将数组剩余的数依次与这两个数比较&#xff0c;如…

进程间关系和守护进程

一. 进程组/作业/会话 1.进程组 每一个进程除了有一个进程ID之外, 还属于一个进程组. 进程是一个或多个进程的集合. 通常, 它们与同一个作业向关联, 可以接收来自同一个终端下的各种命令,信号. 每一个进程组都有唯一的进程组 ID. 每一个进程组都可以有一个组长进程. 组长进程的…

UVa1586

【题目描述】 传送门 【题目分析】 氵 【AC代码】 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstdlib> #include<set> #include<map> #include<vector> …

猴子偷桃问题

http://blog.csdn.net/snow_5288/article/details/52561882 问题描述&#xff1a; /*有一群猴子&#xff0c;去摘了一堆桃子*/ /*商量之后决定每天吃剩余桃子的一半*/ /*当每天大家吃完桃子之后&#xff0c;有个贪心的小猴都会偷偷再吃一个桃子*/ /*按照这样的方式猴子们每天都…

UVa1225

【题目描述】 传送门 【题目分析】 做题做多了慢慢都忘记暴力了&#xff0c;想要快速算出来&#xff0c;找到规律&#xff0c;但是找来找去好复杂的都没有找到&#xff0c;然后写了一个不能再暴力的写法&#xff0c;就过了。。。 我还是觉得如果数据范围变成1e9那种级别的还…

linux 线程学习之条件变量

http://blog.csdn.net/hemmanhui/article/details/4417433 互斥锁&#xff1a;用来上锁。 条件变量&#xff1a;用来等待&#xff0c;当条件变量用来自动阻塞一个线程&#xff0c;直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。 函数介绍&#xff1a; 1&#xff0e;…

UVa455

【题目描述】 传送门 【题目分析】 就是一个简单的暴力&#xff0c;只是需要注意输出格式比较毒瘤。 【AC代码】 #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<cmath> #i…

网络相关基础概念

一. 相关基础概念 1.计算机网络的特点 (1)连通性:计算机网络使得上网的用户都能够彼此相连, 好像用户的计算机可以直接相连     (2)资源共享:资源共享可以是信息共享, 软件共享, 硬件共享等等. 由于网络的存在, 使得用户感觉资源就在自己身边 2. 网络 网络是由若干结点和…

linux线程同步(2)-条件变量

https://www.cnblogs.com/yuuyuu/p/5140875.html linux线程同步(2)-条件变量 一.概述 上一篇&#xff0c;介绍了互斥量。条件变量与互斥量不同&#xff0c;互斥量是防止多线程同时访问共享的互斥变量来保护临界区。条件变量…

UVa227

【题目描述】 传送门 【题目分析】 题目的意思很简单&#xff0c;只是输入输出很毒瘤&#xff0c;我一开始用的fgets然后用scanf(" ")吃掉所有的空格和换行&#xff0c;可是这样有可能将迷宫的空格吃掉&#xff08;例如这个空格恰好在第一行第一列&#xff09;。 …

点对点数据链路层

数据链路层的主要功能将数据转换为相应的比特流使用的信道主要有点对点的信道方式(一对一的方式), 以及广播的信道方式 一. 点对点信道的数据链路层 1. 数据链路和数据帧 链路就是从一个结点连接到相邻结点的一段物理线路(有线或者无线), 期间不准有任何的交换结点, 因此两台…

UVa232

[题目描述] 传送门 [题目分析] 简单的模拟,注意细节 [AC代码] #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype> #include<queue> #include<set>using namespace std;typedef long…

linux线程同步(1)-互斥量

http://www.cnblogs.com/yuuyuu/p/5140251.html 一.概述 互斥量是线程同步的一种机制&#xff0c;用来保护多线程的共享资源。同一时刻&#xff0c;只允许一个线程对临界区进行访问。 互斥量的工作流程&#xff1a;创建一个…

UVa1368

[题目描述] 传送门 [题目分析] 乍一看好像有点复杂,稍微思考一下只需要找到每个位置中最多的碱基.如果相等的话优先输出字典序小的. [AC代码] #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype>…