Art Navsegda
Art Navsegda

Reputation: 45

"cannot convert to a pointer type" with accessing struct via pointer

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

I'm trying to use pointer when copying contents of of struct to another struct via memcpy. But when I transfer pointer to the struct into function I cannot cast it into the 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);
}

Upvotes: 2

Views: 22805

Answers (2)

Jabberwocky
Jabberwocky

Reputation: 50775

You want this:

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

You just forgot the & operator.

BTW: there is no need to cast to (void*) here.

Upvotes: 3

P.P
P.P

Reputation: 121387

You are missing an ampersand (&):

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

(I removed the needless casts).

Upvotes: 9

Related Questions