2013-03-07 22 views
1
options->dict_size = UINT32_C(1) << (uint8_t []){ 
     18, 20, 21, 22, 22, 23, 23, 24, 25, 26 }[level]; 

http://svn.r-project.org/R/trunk/src/extra/xz/lzma/lzma_encoder_presets.c化合物C99字面等效在MSVC

#ifndef UINT32_C 
# if UINT_MAX != 4294967295U 
#  error UINT32_C is not defined and unsigned int is not 32-bit. 
# endif 
# define UINT32_C(n) n ## U 
#endif 

編譯此爲窗戶。但越來越語法錯誤

error C2059: syntax error : '{' 

error C2143: syntax error : missing ';' before '{' 

error C2337: 'level' : attribute not found 

typedef struct { 
    uint32_t dict_size; 
    // ... 
} lzma_options_lzma; 

有沒有人嘗試過呢?

此外,我從來沒有見過像uint8_t []{...}[level]這樣的代碼;

這是什麼意思?

+0

怎麼樣分別聲明數組? – 2013-03-07 09:06:09

+0

我試過..但是const uint32_t level = preset&LZMA_PRESET_LEVEL_MASK;水平已經被宣佈,我無法解釋如何調整(uint8_t [])18,20,21,22,22,23,23,24,25,26} [level]; – Capricorn 2013-03-07 09:10:43

回答

5
const static uint8_t shift_lookup[] = { 
    18, 20, 21, 22, 22, 23, 23, 24, 25, 26 }; 
options->dict_size = UINT32_C(1) << shift_lookup[level]; 

其中shift_lookup是一些尚未在代碼中使用的名稱。

(uint8_t []){...}是一個C99複合文字,它是一個未命名的數組類型的對象。 MSVC不執行C99。爲了在C89中獲得相同的結果,你必須給它一個名字。

(uint8_t []){...}[level]是數組的正常索引操作符,應用於文字。

對於這種特定的情況,如果數組恰好是字符類型,C89實際上已經有一種可以工作的字面量:字符串文字。

options->dict_size = UINT32_C(1) << "\x12\x14\x15\x16\x16\x17\x17\x18\x19\x1a"[level]; 

雖然我不推薦它。

+1

+1爲可怕的字符串使用!:) – unwind 2013-03-07 12:34:03

+0

謝謝史蒂夫。我應該認爲這是非常簡單的 – Capricorn 2013-03-07 12:36:42

4

這是一個C99 compound literal

它定義了一個數組,然後在同一個表達式中將數組索引到數組中。如果level爲零,則它將移位18位,如果它是一位,它將移位20位,依此類推。

如果您試圖使用Visual Studio進行構建,那就是您的問題,因爲Visual Studio不支持C99。

修復它需要重構刪除內聯數組,而不是使其成爲一個常規數組。

+0

如何使用MSVC獲得相同的效果?我嘗試使用類似這樣的東西:options-> dict_size = UINT32_C(1)<<(uint8_t level []){ \t \t \t 18,20,21,22,22,23,23,24,25,26};但沒有運氣。我需要使用開關嗎? – Capricorn 2013-03-07 08:57:26

+0

@Capricorn:As * unwind「寫道:VC不支持複合文字。 – alk 2013-03-07 10:36:50