【C 语言】结构体 ( 结构体 数组 作为函数参数 | 数组 在 堆内存创建 )
生活随笔
收集整理的這篇文章主要介紹了
【C 语言】结构体 ( 结构体 数组 作为函数参数 | 数组 在 堆内存创建 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、結構體 數組 作為函數參數 ( 數組 在 堆內存創建 )
- 二、完整代碼示例
一、結構體 數組 作為函數參數 ( 數組 在 堆內存創建 )
在上一篇博客 【C 語言】結構體 ( 結構體 數組 作為函數參數 | 數組 在 棧內存創建 ) 的基礎上 , 將 棧內存 中的 結構體數組 , 更改為 堆內存 中創建結構體數組 ;
在堆內存中 , 創建 結構體數組 : 傳入 二級指針 , 該指針 指向 結構體 指針 , 傳入 二級指針 的目的是 , 可以在函數中 , 通過形參 間接賦值 , 達到返回創建堆內存的目的 ;
/*** @brief create_student 堆內存中分配內存* @param array 二級指針 , 指向結構體數組* @return*/ int create_student(Student **array, int count) {// 返回值int ret = 0;// 臨時變量Student *tmp = NULL;// 驗證二級指針合法性if(array == NULL){ret = -1;return ret;}// 堆內存中申請內存tmp = (Student *)malloc(sizeof(Student) * count);// 通過間接賦值 設置返回值*array = tmp;return ret; }釋放堆內存中的 結構體 數組 : 傳入的參數是 二級指針 , 通過該 二級指針 指向 結構體一級指針 , 將 結構體指針 置空 ;
/*** @brief free_student 釋放內存* @param array* @return*/ int free_student(Student **array) {// 返回值int ret = 0;// 驗證二級指針合法性if(array == NULL){ret = -1;return ret;}// 釋放內存free(*array);// 指針置空 , 防止野指針*array = NULL;return ret; }二、完整代碼示例
完整代碼示例 :
#include <stdio.h> #include <stdlib.h> #include <string.h>/*** @brief The Student struct* 定義 結構體 數據類型 , 同時為該結構體類型聲明 別名* 可以直接使用 別名 結構體變量名 聲明結構體類型變量* 不需要在前面添加 struct 關鍵字*/ typedef struct Student {char name[5];int age;int id; }Student;/*** @brief printf_struct_array 打印結構體數組* @param array 數組作為函數參數退化為指針* @param count 數組中的元素個數*/ void printf_struct_array(Student *array, int count) {// 循環控制變量int i = 0;// 驗證數組合法性if(array == NULL){return;}// 打印結構體數組中的 結構體 age 字段for(i = 0; i < count; i++){printf("Student age = %d\n", array[i].age);} }/*** @brief sort_struct_array 對結構體數組 按照年齡進行排序* @param array 結構體指針* @param count 結構體數組的元素個數*/ void sort_struct_array(Student *array, int count) {// 循環控制變量int i = 0, j = 0;// 學生年齡Student tmp;// 驗證數組合法性if(array == NULL){return;}// 排序for(i = 0; i < count; i++){for(j = i + 1; j < count; j++){if(array[i].age > array[j].age){tmp = array[i];array[i] = array[j];array[j] = tmp;}}} }/*** @brief create_student 堆內存中分配內存* @param array 二級指針 , 指向結構體數組* @return*/ int create_student(Student **array, int count) {// 返回值int ret = 0;// 臨時變量Student *tmp = NULL;// 驗證二級指針合法性if(array == NULL){ret = -1;return ret;}// 堆內存中申請內存tmp = (Student *)malloc(sizeof(Student) * count);// 通過間接賦值 設置返回值*array = tmp;return ret; }/*** @brief free_student 釋放內存* @param array* @return*/ int free_student(Student **array) {// 返回值int ret = 0;// 驗證二級指針合法性if(array == NULL){ret = -1;return ret;}// 釋放內存free(*array);// 指針置空 , 防止野指針*array = NULL;return ret; }/*** @brief 主函數入口* @return*/ int main(int argc, char* argv[], char**env) {// 聲明結構體數組 , 該數組在棧內存中Student *array = NULL;// 循環控制變量int i = 0;// 堆內存中為結構體指針分配內存create_student(&array, 3);// 命令行中 , 接收輸入的年齡for(i = 0; i < 3; i++){printf("\n Input Age :\n");// 命令換行中 接收 輸入的年齡 ,// 設置到 Student 數組元素的 age 成員中scanf("%d", &(array[i].age));}// 結構體數組 按照 age 排序sort_struct_array(array, 3);// 打印結構體數組中的 結構體 age 字段printf_struct_array(array, 3);// 釋放堆內存數據free_student(&array);// 命令行不要退出system("pause");return 0; }執行結果 :
Input Age : 12Input Age : 11Input Age : 14 Student age = 11 Student age = 12 Student age = 14 請按任意鍵繼續. . .總結
以上是生活随笔為你收集整理的【C 语言】结构体 ( 结构体 数组 作为函数参数 | 数组 在 堆内存创建 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C 语言】结构体 ( 结构体变量内存操
- 下一篇: 【C 语言】结构体 ( 结构体中嵌套一级