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

熱點內容
c語言不可嵌套的是 發布:2025-07-01 13:04:33 瀏覽:327
三星手機老是顯示存儲空間不足 發布:2025-07-01 13:00:09 瀏覽:593
ewebeditor上傳漏洞 發布:2025-07-01 13:00:08 瀏覽:834
電腦無解壓 發布:2025-07-01 12:51:18 瀏覽:732
中國股票市場交易資料庫 發布:2025-07-01 12:43:23 瀏覽:622
oppoa37怎麼給應用加密 發布:2025-07-01 12:20:53 瀏覽:644
java毫秒數 發布:2025-07-01 12:19:14 瀏覽:520
評委打分c語言 發布:2025-07-01 12:10:05 瀏覽:145
c語言中e的次方 發布:2025-07-01 12:02:23 瀏覽:179
php訪問量統計代碼 發布:2025-07-01 11:57:56 瀏覽:211