Passing entire struct array to function

Dragonmoon

I'm learning C++ and graphics programming, and following a tutorial, but I can't seem to be able to find any solutions to this problem.

This is the code:

struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };

void writetovram(struct CUSTOMVERTEX *verticies)
{
    ...
    memcpy(pVoid, verticies, sizeof(verticies));
    ...
    return;
}

void createshapes() 
{
    //simple square
    CUSTOMVERTEX verticies[] =
    {
        { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

    };
    writetovram(&verticies);
}

I need to be able to pass this struct array in its entirety as a pointer to the function. I've found that if I havn't used pointers then the data doesn't get transferred properly.

I've tried:

void writetovram(struct CUSTOMVERTEX *verticies)
void writetovram(struct CUSTOMVERTEX verticies)
void writetovram(struct CUSTOMVERTEX &verticies)
void writetovram(struct CUSTOMVERTEX *verticies[])
void writetovram(struct CUSTOMVERTEX verticies[])

and

writetovram(&verticies);
writetovram(*verticies);
writetovram(verticies);

Edit: When I put the code like this, it works:

void writetovram()
{
    CUSTOMVERTEX verticies[] =
    {
        { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

    };
    ...
    memcpy(pVoid, verticies, sizeof(verticies));
    ...
    return;
}

Basically, all the combinations of pointers and syntax I could think of. But none have worked.

Can anyone help?

I've also searched online and found none having this problem.

Remy Lebeau

Inside of writetovram(), sizeof(verticies) doesn't return what you think it does. You can't get the size of an array from just a pointer. There are TONS of questions on StackOverflow on this issue.

Your simplest option is to just pass the array size as another parameter, eg:

struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };

void writetovram(CUSTOMVERTEX *verticies, int num_verticies)
{
    ...
    memcpy(pVoid, verticies, sizeof(*verticies) * num_verticies);
    ...
}

void createshapes() 
{
    //simple square
    CUSTOMVERTEX verticies[] =
    {
        { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

    };

    writetovram(verticies, 4);
    // or:
    // writetovram(verticies, sizeof(verticies)/sizeof(verticies[0]));
    // or:
    // writetovram(verticies, std::size(verticies));
}

Other options include:

  • pass the array by reference, but only if the size is known at compile-time. But if multiple different-sized arrays need to be used, then writetovram() would have to be made into a template function so the array size can be deduced at compile-time for each call site.

  • changing the array to use std::array or std:vector instead, passed to the function by reference, and then it can use their size() methods.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing an array of 'typedef struct' to a function

From Dev

Passing array of struct to function in C

From Dev

passing struct array as parameter to a function

From Dev

Passing char array to a struct array within a function

From Dev

Passing malloc struct array through a function

From

Passing dynamic array struct to a function Golang

From Dev

c++ passing struct array to function

From Dev

Passing struct array to function and calculating average in C?

From Dev

Passing an array from a struct as an argument to a function

From Dev

passing a struct argument in a struct function

From Dev

In JavaScript, is there any way of passing elements of an array into a function without passing in the entire array?

From Dev

struct function passing and returning

From

Passing Struct array elements as parameters to a function in Go language

From Dev

Passing a function out of a struct to a function

From Dev

Passing struct parameter to a function inside a struct

From Dev

Passing struct/union to a function in C

From

Passing in dynamic struct into function in golang

From

Passing go embedded struct to function

From

Go - passing generic struct to function

From Dev

Passing a large const struct to a function

From Dev

Passing struct to a function to assign values

From Dev

Passing a classA struct into an outside function

From Dev

Passing pointer to a member of a struct to a function

From Dev

Passing struct members to function in c

From

passing a slice/array to another struct

From Dev

passing array (not pointer) to a struct in c

From Dev

Passing array with fixed size to struct

From Dev

Passing a struct to construct an array of Structs

From Dev

Passing entire select statement as a parameter - PostgreSQL function

Related Related

  1. 1

    Passing an array of 'typedef struct' to a function

  2. 2

    Passing array of struct to function in C

  3. 3

    passing struct array as parameter to a function

  4. 4

    Passing char array to a struct array within a function

  5. 5

    Passing malloc struct array through a function

  6. 6

    Passing dynamic array struct to a function Golang

  7. 7

    c++ passing struct array to function

  8. 8

    Passing struct array to function and calculating average in C?

  9. 9

    Passing an array from a struct as an argument to a function

  10. 10

    passing a struct argument in a struct function

  11. 11

    In JavaScript, is there any way of passing elements of an array into a function without passing in the entire array?

  12. 12

    struct function passing and returning

  13. 13

    Passing Struct array elements as parameters to a function in Go language

  14. 14

    Passing a function out of a struct to a function

  15. 15

    Passing struct parameter to a function inside a struct

  16. 16

    Passing struct/union to a function in C

  17. 17

    Passing in dynamic struct into function in golang

  18. 18

    Passing go embedded struct to function

  19. 19

    Go - passing generic struct to function

  20. 20

    Passing a large const struct to a function

  21. 21

    Passing struct to a function to assign values

  22. 22

    Passing a classA struct into an outside function

  23. 23

    Passing pointer to a member of a struct to a function

  24. 24

    Passing struct members to function in c

  25. 25

    passing a slice/array to another struct

  26. 26

    passing array (not pointer) to a struct in c

  27. 27

    Passing array with fixed size to struct

  28. 28

    Passing a struct to construct an array of Structs

  29. 29

    Passing entire select statement as a parameter - PostgreSQL function

HotTag

Archive