小编Art*_*gda的帖子

通过指针访问结构“无法转换为指针类型”

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
Run Code Online (Sandbox Code Playgroud)

我试图通过memcpy将struct的内容复制到另一个struct时使用指针。但是,当我将指向该结构的指针转换为函数时,无法将其强制转换为空白。

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; …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

0
推荐指数
1
解决办法
8057
查看次数

标签 统计

c ×1

pointers ×1

struct ×1