當前位置:首頁 » 編程語言 » 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-02 00:12:28 瀏覽:345
shell腳本需要編譯鏈接 發布:2025-07-02 00:04:20 瀏覽:474
微信如何重設密碼 發布:2025-07-02 00:02:27 瀏覽:545
java代碼基礎 發布:2025-07-02 00:00:46 瀏覽:304
煙花的代碼c語言 發布:2025-07-01 23:56:04 瀏覽:224
安卓默認打開文件方式怎麼修改 發布:2025-07-01 23:30:38 瀏覽:862
壓縮機接線座 發布:2025-07-01 23:17:48 瀏覽:662
iqoo瀏覽器緩存路徑 發布:2025-07-01 23:12:38 瀏覽:693
明日之後如何獲得最新伺服器 發布:2025-07-01 23:12:35 瀏覽:52
tv加密頻道 發布:2025-07-01 23:10:58 瀏覽:625