• Advertisement
Sign in to follow this  

Passing a struct

This topic is 3035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! Im busy coding something in C++ using the Direct3D API and i have defined a custom vertex structure containing the vertices of a cube like so... CVERTEX g_cubeVertices[] = {/*verts here*/}; And i need to pass that list of vertices through the Render function to the memcpy function, something simmilar to.. // g_cubeVertices Render(CVERTEX pVerts) { memcpy(pVertices,pVerts,sizeof(pVerts)); } But obviously that doesnt work because i get the error: "Cant convert from CVERTEX to const void*" Can anyone give me advice or a workaround for this.. Im not exactly a very good programmer and its driving me crazy! :-/ Thanks!!!

Share this post


Link to post
Share on other sites
Advertisement
You shouldn't be able to pass your list to a function taking only a CVERTEX. Always post the actual code you compile, as it's hard to say exactly what the problem is here. But for the posted code, use CVERTEX *pVerts instead, as you need a pointer to the list. Just pass the list as Render(g_cubeVertices) and the array will automatically decay to a pointer.

Share this post


Link to post
Share on other sites
Your function is declared as taking a single "CVERTEX " instance. You should write it so it takes an array or pointer:

void Render(CVERTEX *vertexArray, int vertexCount)
{
// ...

memcpy(pVertices, vertexArray, sizeof(CVERTEX) * vertexCount);

// ...
}


Be careful with sizeof(). For example, in the above code were I to use sizeof(vertexArray) on most machines it would only be 4 bytes, because that is the size of the pointer, not the pointed at instance. Use std::copy where possible.

Share this post


Link to post
Share on other sites
Hey thanks! That seemed to work. It didnt work at first but then i found that i had an externally defined Verts object that had a different return type. Thanks again!

Share this post


Link to post
Share on other sites
Hey thanks! That seemed to work. It didnt work at first but then i found that i had an externally defined Verts object that had a different return type. Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement