close

在轉換人臉辨識專題用程式時, 為了要加入File copy的code, 

要把GCC compiler  改用G++ 來build (openCV 等都會用到C++功能)

但是會出現以下error

mysock1.c: In function 'size_t writen(int, const void*, size_t)':
mysock1.c:88:6: error: invalid conversion from 'const void*' to 'const char*

在這裏面 writen 是目前還沒用到的函式所以先不看,我對void pointer 倒是有點興趣

void pointer 需要做cast, 不過不能直接dereference 或做運算

https://stackoverflow.com/questions/692564/concept-of-void-pointer-in-c-programming

void indicates the absence of type, it is not something you can dereference or assign to.

較合適的範例用法

/* may receive wrong value if ptr is not 2-byte aligned */
uint16_t value = *(uint16_t*)ptr;
/* portable way of reading a little-endian value */
uint16_t value = *(uint8_t*)ptr
                | ((*((uint8_t*)ptr+1))<<8);

https://stackoverflow.com/questions/34842224/when-to-use-const-void/34842262

這篇的解答是有關constant cast的問題

const and void are orthogonal. That is all the discussion above about const applies to any type (int, char, void, ...)

void * is used in C for "generic" data.

所以結論是unless you really know what you are doing, and all the people working in the present and in the future on the code are experts and understand this, and you have a good motivation, unless all the above are met, never cast away the constness!!

 

 

 

arrow
arrow
    全站熱搜

    紅線 發表在 痞客邦 留言(0) 人氣()