當前位置:首頁 » 編程語言 » 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;}望採納!

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:573
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:863
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:561
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:743
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:663
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:984
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:233
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:92
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:785
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:691