實際上,我想知道如何將數組值用於其他數組的大小聲明。我有這樣的測試代碼:使用數組中的值聲明另一個數組的大小
main(){
int sz[3]={1,2,3};
int track1[ sz[0] ]={111};
int track2[ sz[1] ]={222,222};
int track3[ sz[2] ]={333,333,333};
printf("%d %d %d\n", track1[0],track2[1],track3[2]);
}
這些都是警告和錯誤,我得到:
test2.c: In function ‘main’:
test2.c:4: error: variable-sized object may not be initialized
test2.c:4: warning: excess elements in array initializer
test2.c:4: warning: (near initialization for ‘track1’)
test2.c:5: error: variable-sized object may not be initialized
test2.c:5: warning: excess elements in array initializer
test2.c:5: warning: (near initialization for ‘track2’)
test2.c:5: warning: excess elements in array initializer
test2.c:5: warning: (near initialization for ‘track2’)
test2.c:6: error: variable-sized object may not be initialized
test2.c:6: warning: excess elements in array initializer
test2.c:6: warning: (near initialization for ‘track3’)
test2.c:6: warning: excess elements in array initializer
test2.c:6: warning: (near initialization for ‘track3’)
test2.c:6: warning: excess elements in array initializer
test2.c:6: warning: (near initialization for ‘track3’)
test2.c:8: warning: incompatible implicit declaration of built-in function ‘printf’
這裏有什麼問題,ovarall這是甚至可能的,什麼我沒試過嗎?
你正在編譯爲C或C++嗎? –
請參閱:http://stackoverflow.com/questions/3082914/c-compile-error-variable-sized-object-may-not-be-initialized對於類似的問題。 – lsk