2015-08-31 109 views
0

我想從這個靜態變量中獲得一個常量變量。如何從C中的靜態變量聲明一個常量變量?

#define video_mode_count 12 
static freenect_frame_mode supported_video_modes[video_mode_count] = { 
    // reserved, resolution, format, bytes, width, height, data_bits_per_pixel, padding_bits_per_pixel, framerate, is_valid 
    {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_RGB), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_RGB}, 1280*1024*3, 1280, 1024, 24, 0, 10, 1 }, 
    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_RGB}, 640*480*3, 640, 480, 24, 0, 30, 1 }, 

    {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_BAYER), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_BAYER}, 1280*1024, 1280, 1024, 8, 0, 10, 1 }, 
    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_BAYER), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_BAYER}, 640*480, 640, 480, 8, 0, 30, 1 }, 

    {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_8BIT), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_8BIT}, 1280*1024, 1280, 1024, 8, 0, 10, 1 }, 
    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_8BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_8BIT}, 640*488, 640, 488, 8, 0, 30, 1 }, 

    {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_10BIT), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_10BIT}, 1280*1024*2, 1280, 1024, 10, 6, 10, 1 }, 
    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_10BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_10BIT}, 640*488*2, 640, 488, 10, 6, 30, 1 }, 

    {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_10BIT_PACKED), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_10BIT_PACKED}, 1280*1024*10/8, 1280, 1024, 10, 0, 10, 1 }, 
    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_10BIT_PACKED), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_10BIT_PACKED}, 640*488*10/8, 640, 488, 10, 0, 30, 1 }, 

    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_YUV_RGB), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_YUV_RGB}, 640*480*3, 640, 480, 24, 0, 15, 1 }, 

    {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_YUV_RAW), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_YUV_RAW}, 640*480*2, 640, 480, 16, 0, 15, 1 },}; 

現在我需要寫的東西

const FREENECT_VIDEO_RGB = [ An instance in the type of freenect_frame_mode ] 

如何聲明該格式一個const變種?


原代碼來自:

https://github.com/OpenKinect/libfreenect/blob/master/src/cameras.c#L42

我需要傳遞一個const變種freenect_frame_mode成這樣了,看看它是否返回-1與否:

https://github.com/OpenKinect/libfreenect/blob/master/src/cameras.c#L1152

int freenect_set_video_mode(freenect_device* dev, const freenect_frame_mode mode) 
{ 
freenect_context *ctx = dev->parent; 
if (dev->video.running) { 
    FN_ERROR("Tried to set video mode while stream is active\n"); 
    return -1; 
} 
// Verify that the mode passed in is actually in the supported mode list 
int found = 0; 
int i; 
for(i = 0 ; i < video_mode_count; i++) { 
    if (supported_video_modes[i].reserved == mode.reserved) { 
     found = 1; 
     break; 
    } 
} 
if (!found) { 
    FN_ERROR("freenect_set_video_mode: freenect_frame_mode provided is invalid\n"); 
    return -1; 
} 

freenect_resolution res = RESERVED_TO_RESOLUTION(mode.reserved); 
freenect_video_format fmt = (freenect_video_format)RESERVED_TO_FORMAT(mode.reserved); 
dev->video_format = fmt; 
dev->video_resolution = res; 
// Now that we've changed video format and resolution, we need to update 
// registration tables. 
freenect_fetch_reg_info(dev); 
return 0; 

}

我只是堅持使用現在的功能。

謝謝!

+0

一個常量變量是definitelly不可變.... :)那麼'const靜態...'呢? –

回答

3

那麼,如果你想獲得一個數組的元素,你可以使用標準語法array[index]

const freenect_frame_mode FREENECT_VIDEO_RGB = supported_video_modes[5]; 

例如。

如果您需要更多信息,您需要顯示更多代碼,即這些符號的定義。

+0

該帖子已被重新編輯。我使用上面報告的代碼:'src/input/Freenect.cc:88:27:error:'const freenect_frame_mode FREENECT_VIDEO_RGB'被重新聲明爲不同種類的符號 const freenect_frame_mode FREENECT_VIDEO_RGB = supported_video_modes [5];' – dotslash

+0

好。我已經解決了這個問題。我在這裏得到的錯誤是因爲var * FREENECT_VIDEO_RGB *被聲明瞭兩次。我重命名了var名稱,它的工作原理!謝謝你,克里斯貝克! – dotslash