當前位置:首頁 » 編程語言 » c語言動態與結構體

c語言動態與結構體

發布時間: 2025-01-15 00:19:16

c語言問題(student*)malloc(sizeof(student))

C語言動態申請數組(結構體)空間的應用,以下面一段代碼為例,你可以參考一下。
#include<stdio.h>#include<stdlib.h>#include<malloc.h>//包含malloc函數的頭文件struct Item//定義結構體{ char name[20]; int num; };struct Item *I;//申請Item類型的指針int A()//進行內存申請{ int i,j; int n;//申請的數量 scanf("%d",&n); I=(struct Item*)malloc(sizeof(struct Item)*n); return n; }int Input()//數據輸入函數{ int n=A(); int i,j; for(i=0;i<n;i++) { printf("%d Name:",i+1); scanf("%s",&I[i].name); printf("num:\n"); scanf("%d",&I[i].num); } return n; }void output()//數據輸出函數{ int n=Input(); for(int i=0;i<n;i++) { printf("%d |",i); printf("Name:%s ",I[i].name); printf("num:%d\n",I[i].num); }}int main(){ output(); return 0;}望採納!

熱點內容
隨機啟動腳本 發布:2025-07-05 16:10:30 瀏覽:516
微博資料庫設計 發布:2025-07-05 15:30:55 瀏覽:19
linux485 發布:2025-07-05 14:38:28 瀏覽:299
php用的軟體 發布:2025-07-05 14:06:22 瀏覽:751
沒有許可權訪問計算機 發布:2025-07-05 13:29:11 瀏覽:425
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:687
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:234
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:680
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:277
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:828