2016-11-23 115 views
0
gcc -g -O2 struct.c -o struct 
struct.c: In function ‘secondfunction’: 
struct.c:19:2: error: cannot convert to a pointer type 
    firstfunction((void *)onedata->c,(void *)&twodata.c,2); 
    ^~~~~~~~~~~~~ 
<builtin>: recipe for target 'struct' failed 
make: *** [struct] Error 1 

我试图通过memcpy复制结构的内容到另一个结构时使用指针。但是当我将指针传递给函数结构时,我不能将它转换为void。“无法转换为指针类型”通过访问结构通过指针

struct one { 
    char a; 
    char b; 
}; 

struct two { 
    struct one c; 
    struct one d; 
}; 

void firstfunction(void *source, void *dest, int size) 
{ 
    //memcpy(dest,source,size) 
} 

void secondfunction(struct two *onedata) 
{ 
    struct two twodata; 
    firstfunction((void *)onedata->c,(void *)&twodata.c,2); 
} 

void main() 
{ 
    struct two onedata; 
    secondfunction(&onedata); 
} 
+1

作为一个方面说明,它通常被认为是不好的形式来做'void main()'而不是'int main()' – theKunz

+1

不要在没有需要的情况下使用void *。并且不要向'void *'投射或从'void *'投射。 – Olaf

回答

4

你缺少符号(&):

firstfunction(&onedata->c, &twodata.c,2); 
      ^

(我删除了不必要的铸)。

2

你想这样的:

void secondfunction(struct two *onedata) 
{ 
    struct two twodata; 
    firstfunction(&onedata->c, &twodata.c, 2); 
} 

你刚才忘了&操作。

顺便说一句:这里没有必要投到(void*)