我有一个以下类型的结构
typedef struct Edge { int first; int second; }Edge;
我在我的main函数中实例化并复制到数组中
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)); }
我一直遇到以下错误.
src/main.cu(28): error: no suitable conversion function from "Edge" to "void *" exists
第28行是memcpy发生的行.任何帮助赞赏.
您根本不需要使用malloc
或根本不需要memcpy
.你可以:
Edge h_edges[NUM_EDGES]; for (int i = 0; i < NUM_VERTICES; ++i) // or should this be NUM_EDGES?? { h_edges[i].first = (rand() % (NUM_VERTICES+1)); h_edges[i].second = (rand() % (NUM_VERTICES+1)); }