這里給出他的寫法
#include#includestruct ss
{int n; // 變長數(shù)組的長度
char a[0]; // 不可寫為char *a, 否則會報錯
// 注意這里的a[0]不占內(nèi)存,但是*a占內(nèi)存
// 因為sizeof(a) == 0, 所以需要一個n來給出當(dāng)前變長數(shù)組的長度。
};
int main()
{ int n;
n = 10;
int saf[n]; // 這不是一個變長數(shù)組,因為一旦定義之后,就不能改變大小
printf("%ld",sizeof(saf));
// 定義變長數(shù)組大小為n
struct ss *b = (struct ss *)malloc(sizeof(struct ss) + n * sizeof(char));
b->n = n;
// 現(xiàn)在改變數(shù)組的長度
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);
// 變長數(shù)組的大小不能使用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
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站標(biāo)題:c語言變長數(shù)組的實現(xiàn)-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://jinyejixie.com/article36/ddcjpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、標(biāo)簽優(yōu)化、建站公司、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容