當前位置:首頁 » 編程語言 » 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-01 04:37:35 瀏覽:429
小雞g4怎麼連安卓 發布:2025-07-01 04:37:25 瀏覽:353
黃金線主圖源碼 發布:2025-07-01 04:35:38 瀏覽:296
阿里輕量伺服器有固定IP嗎 發布:2025-07-01 04:30:27 瀏覽:504
電腦什麼配置要合適 發布:2025-07-01 04:24:15 瀏覽:164
哪個安卓恢復工具不要錢 發布:2025-07-01 04:21:54 瀏覽:740
sql加空格 發布:2025-07-01 04:09:38 瀏覽:578
如何關閉ftp防篡改 發布:2025-07-01 04:09:04 瀏覽:90
頑固的緩存 發布:2025-07-01 03:28:23 瀏覽:114
u盤插安卓手機上怎麼加密 發布:2025-07-01 03:09:19 瀏覽:91