I've a struct of the following type
typedef struct Edge
{
int first;
int second;
}Edge;
Which I'm instantiating in my main function and copying into an array
Edge h_edges[NUM_EDGES];
for (int i = 0; i < NUM_VERTICES; ++i)
{
Edge* e = (Edge*)malloc(sizeof(Edge));
e->first = (rand() % (NUM_VERTICES+1));
e->second = (rand() % (NUM_VERTICES+1));
memcpy(h_edges[i], e, sizeof(e));
}
I keep running into the following error.
src/main.cu(28): error: no suitable conversion function from "Edge" to "void *" exists
Line 28 is the line where the memcpy happens. Any help appreciated.
h_edges[i]of typeEdgeinmemcpywhich is expecting avoid *. You can't use a struct type in place of a pointer type. You needmemcpy(&h_edges[i], e, sizeof(*e)). In other words, the address ofh_edges[i]is what you're copying to, and you want the size of whatepoints to not the size ofeitself, which is a pointer (you'd get the size of a pointer). You could also usesizeof(Edge)there.malloc. Just use,Edge* e = malloc(sizeof(Edge));.mallocshould not generate an error. If it does, there's something else wrong.Edge *e = malloc(sizeof(Edge));should work just fine. See this post for details on why you shouldn't cast it.malloc. Of course it isn't at all obvious why you are usingmallocin that code snippet at all.