這里給出他的寫法

#include#includestruct ss
{int n; // 變長數組的長度
char a[0]; // 不可寫為char *a, 否則會報錯
// 注意這里的a[0]不占內存,但是*a占內存
// 因為sizeof(a) == 0, 所以需要一個n來給出當前變長數組的長度。
};
int main()
{ int n;
n = 10;
int saf[n]; // 這不是一個變長數組,因為一旦定義之后,就不能改變大小
printf("%ld",sizeof(saf));
// 定義變長數組大小為n
struct ss *b = (struct ss *)malloc(sizeof(struct ss) + n * sizeof(char));
b->n = n;
// 現在改變數組的長度
n = 100;
b = (struct ss *)malloc(sizeof(struct ss) + n * sizeof(char));
b->n = n;
char c[10] = "wfasfa";
memcpy(b->a,c,sizeof(c));
printf("%s\n",b->a);
// 變長數組的大小不能使用sizeof得到
printf("%ld\n",sizeof(b->a));
struct ss *d = (struct ss *)malloc(sizeof(struct ss) + 10 * sizeof(char ));
return 0;
}
參考自:
https://stackoverflow.com/questions/3607859/meaning-of-char-arr0
https://blog.csdn.net/hyqsong/article/details/50863746
https://blog.csdn.net/ligeforrent/article/details/48639277
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
網站標題:c語言變長數組的實現-創新互聯
網站URL:http://www.yijiale78.com/article36/ddcjpg.html
成都網站建設公司_創新互聯,為您提供網站改版、面包屑導航、響應式網站、標簽優化、建站公司、網站導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯