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.pswp.cn/news/384027.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384027.shtml
英文地址,請注明出處:http://en.pswp.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>…