Edward Lee
|
Re: C syntax with a struct array
|
Edward Lee
08/01/2013 10:51 AM
post103842
|
Re: C syntax with a struct array
Okay, I was pre-occupied with your word alignment issue ... now looking at your declarations ... I see it now, this is
something that I have encountered before.
When you create an array of data structures, the variable becomes a pointer to the first element of the array, or a
pointer to the first CommandeCVS structure in a sequence of MAXLIGNECVS structures.
When pass it in as "CommandesCVS * commandesCVS", commandesCVS is now a pointer to an array of data structures. which is
a pointer to a pointer of the structure.
So when you perform commandesCVS[ctLigne]->X, you are starting with the pointer to the pointer, and then offsetting it
by ctLigne * sizeof (CommandeCVS), which will now point to somewhere weird.
You need to dereference the "pointer to the pointer" back to just the pointer, which is why when you perform (*
commandesCVS)[ctLigne].X, it works. The (*commandesCVS) will perform the dereferencing, and you are now back to the
structure array.
You really do not need to pass in "CommandesCVS * commandesCVS", you can pass in "CommandesCVS commandesCVS", since it
is a pointer already. You only need the former type of declaration if you have multiple CommandesCVS, and lectureCVS
will reassign *commandesCVS to point to a different CommandesCVS.
I recommend that you change it to:
Uint32 lectureCVS (Char * nomFichier, CommandesCVS commandesCVS)
Then, you will use commandesCVS[ctLigne].X = blah;
However, I seriously recommend you change your data structure. Using what you have right now, if you were to do:
commandesCVS[1].data = blah;
you will be writing a 32-bit word into (base_addr + 0x6), which you cannot do; the processor will throw an address
exception error, and your process will get kicked out by the kernel, and you will get a core dump (if dumper is running)
.
hope this helps!!
- Ed
|
|
|