David Rasmussen posted:
Normally, array assignments aren't possible/allowed. If I do s1.arr =
s2.arr I get a compiler error.
That's exactly like writing:
5 = 6;
"Hello" = "Goodbye";
As I'm about to explain:
unsigned int Ages[7];
Now, if I write:
unsigned int k = Ages[0];
Then the value stored in the variable entitled "Ages[0]" gets copied to the
variable entitled "k". No problemo. Now... look at the following:
k = Ages;
"Ages" written on it's own, without an array index, is exactly like writing
"&Ages[0]", ie. it's equal to the address of the first variable in the
array. Now, take your example:
s1.ar = s2.ar;
That is equal to:
&s1.ar[0] = &s2.ar[0];
That's exactly like writing:
unsigned int k = 4;
&k = 27873;
You're not changing the value of a variable at all! You're trying to change
the address of a variable! Self-explanatory I hope!
If I do s1 = s2 it compiles fine.
unsigned int a = 5;
unsigned int b = 6;
a = b;
b = a;
&a = &b; //ERROR
All you are doing is copying memory! As so:
S s1;
S s2;
s1 = s2;
s2 = s1;
&s1 = &s2; //ERROR
Now, let's try solve your problemo:
unsigned int Cats[48];
unsigned int Dogs[48];
To copy the whole lot, you'd have to do:
Cat[0] = Dog[0];
Cat[1] = Dog[1];
Cat[2] = Dog[2];
Cat[3] = Dog[3];
Why do we have to do this? Because there's no variable called "Cat", nor is
there a varible called "Dog". There's a variable called "Cat[0]" alright, as
is there a variable called "Dog[0]". So why is the following valid?:
unsigned int* pCats = Cats;
There's no reason! It makes no sense at all! Someone just decided it was
more convenient than:
unsigned int* pCats = &Cats[0];
I'll get to the point, how should we do it? Well, what you've done is
actually very clever and it's the best thing I've ever seen for doing it!
Take the following:
void RegisterName(const char* const pNameToRegister)
{
//Here, we need to keep a record of the name, so we copy it.
//Normally, I would use strcpy, as follows:
char NameBuffer[30];
strcpy(NameBuffer, pNameToRegister);
//But you've come up with a brilliant idea!!
struct StringCopierStructureBuffer
{
char NameBuffer[30];
};
StringCopierStructureBuffer* pInput = (StringCopierStructureBuffer*)
pNameToRegister;
StringCopierStructureBuffer pStored;
pStored = *pInput;
//We don't need strcpy! We can use this with any sort of array!
//I wrote the above code in about 5 mins and there's likely to be a
//thousand errors in it, but you get the picture.
}
-JKop