2

I have a struct array like this:

struct VERTEX_FMT
{
    float x, y, z, u, v;
    const static DWORD FVF = (D3DFVF_XYZ | D3DFVF_TEX1);
};
VERTEX_FMT vertices[] = {
    {-1.0f, -1.0f,  0.0f,  0.0f,  0.0f},
    {-1.0f,  1.0f,  0.0f,  0.0f,  1.0f},
    { 1.0f, -1.0f,  0.0f,  1.0f,  0.0f},
    { 1.0f,  1.0f,  0.0f,  1.0f,  1.0f},
};

Is there an easy to way assign the struct array a new value in C++.

1
  • 4
    Not in C++03, which inherited C's way to treat arrays as 3rd-class citizens of the language. C++11, however, will bring uniform initialization syntax, and that should help. Commented Jun 8, 2011 at 7:23

4 Answers 4

2

Only if you wrap the array in another struct. Arrays are pretty broken in C, and C++ decided that std::vector was sufficient, and that any attempt to fix C style arrays either wouldn't go far enough to make a difference, or would break compatibility to a point where you couldn't talk of C style arrays any more.

For POD arrays like yours, memcpy is by far the simplest solution. For more complicated cases, and even most cases of arrays of structs like yours, you should probably consider using std::vector instead. Or a mixture: use the C style arrays for static, initialized arrays such as the one you show, so that the compiler will count the number of elements for you, but use std::vector for anything which isn't completely initialized in the definition, or which is the target of an assignment. It's easy to construct the std::vector from a C style array using the two iterator constructor of std::vector, so there's no real inconvenience in having both types.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I realize that std::vector is much more powerful than C arrays, the reason to use C arrays here is that I must use memcpy to copy the whole memory somewhere. I wonder there is some kind of tricks to change the values. BTW, what is POD arrays?
@Nickolas A POD array is an array of POD types. Roughly speaking, types which could exist in C (and are memcpyable). With regards to std::vector, if the instantiation type is a POD, you can memcpy it with something like memcpy(&v[0], dest, v.size() * sizeof(v[0])). Just make sure it's not empty beforehand.
1

if you mean insert a new value, then no - the array is fixed size - you cannot change it easily. You'll have to create a completely new array - what you should really do is look at std::vector

std::vector<VERTEX_FMT> new_array(vertices, vertices + sizeof(vertices)/sizeof(VERTEX_FMT));
// add the new entry in
new_array.push_back(...);

EDIT: based on the comment, it appears that what you want to do is something like:

vertices[2] = {....}; // new values.

Quickest way to do this in the current standard is to use a std::memcpy or std::copy, something like:

VERTEX_FMT nv = { ... };
// copy this in
std::memcpy(&vertices[2], &nv, sizeof(nv));
// the line below also works if you want to use purely standard algorithms.
//std::copy(&nv, (&nv) + 1, &vertices[2]);

1 Comment

Maybe I mislead you, I mean keep the size, just change its value someway as a whole.
0

You can use std::generate, or some other stl algorithm function.

As already said, there are no other ways to do it in the current version c++ (it will be possible in c++0x)

Comments

0

In current standard, you cannot do,

int a[3] = {1,2,3};
a[] = {4,5,6};

But you can definitely get its effect using pointer to an array type,

int a[3] = {1,2,3}, (*p)[3]; // <-- this syntax forces p to point only int[3]
p = &a;
int b[3] = {4,5,6,};
p = &b;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.