Re: [問題] 請問c可以寫非對稱的多維陣列嗎??
看板C_and_CPP作者softwind (software everywhere)時間13年前 (2011/07/09 01:08)推噓0(0推 0噓 3→)留言3則, 2人參與討論串4/4 (看更多)
※ 引述《spiderman007 (千里之外)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: dev-c
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: 問題(Question):
: 如題...
: 可以將資料用二維陣列寫入非對稱的多維陣列嗎?? 如下
: 1
: 1 2
: 1 2 3
: 1 2 3 4
: 再show出來嗎??
: 餵入的資料(Input):
: 預期的正確結果(Expected Output):
: 錯誤結果(Wrong Output):
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: 補充說明(Supplement):
XD 終於有小弟可以回的問題了~~~
這個嘛... 我是不知道 大部分人怎麼做啦
但是 我接過一些些案子 看過一些些 platform or 3rd lib.
對於長度未定的array 在C裡面 最好的解法是 sized array.
ex:
struct myStruct; // 前置宣告
struct szArray{
unsigned int len; //這個放長度
struct myStruct *pData; // 這個放 pointer指向instance.
};
so 我想你的要求 code類似這樣
struct szArray{
unsigned int len;
int *pData;
};
struct szArray my_array[4];
for( int i=0; i<4; i++){
szArray *pa=&my_array[i];
pa->len=i+1;
pa->pData = calloc( sizeof(int), i+1 );
/* init internal array */
}
------ how to show? -------
for( int i=0; i<4; i++){
szArray *pa=&my_array[i];
for( int j=0; j<pa->len; j++)
printf("%d ", pa->pData[j];
printf("\n");
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.170.64.171
→
07/09 06:27, , 1F
07/09 06:27, 1F
→
07/09 06:29, , 2F
07/09 06:29, 2F
→
07/09 09:22, , 3F
07/09 09:22, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):