Zepper wrote:
Code:
//this
static STRU_T mystru;
//OR...
static STRU_T tmp;
static STRU_T *mystru = &tmp;
Definitely the first one. Why would you want to use indirect access when you can access the variable directly? Direct access is smaller and faster (at least for global variables. Local variables are internally accessed via stack pointer anyway, but even here there's no reason to add yet another pointer.)
Pointers only make sense here if the pointer is the parameter variable of a function:
Code:
void SomeFunction(MyStruct *pStruct)
Zepper wrote:
2. memcpy() or memset() VS "manual" set to zero.
memset if you want to set all bytes to the same value.
Take care if your array is of type int (or any other non-byte type) and you want to initialize with anything non-zero: The value that you set refers to every single byte, not every array item:
Code:
int array[5];
memset(array, 1, 5 * sizeof(int));
This will
not set all five values to 1. It will set all separate
bytes to 1.
So, if the int consists of two bytes, then each array item will now have the value 257 (0x0101).
memcpy if you want to copy data from another array.
(Using memcpy to initialize with 0 or any other single byte value makes no sense. Where do you take the source data from? Do you have some constant array with all zeroes in it that's only there to initialize other arrays with 0 by using it with memcpy?)
And for everything else (for example if you really have an int array that you want to initialize with a certain value per entry and not per byte), you should use a for loop and not initialize every entry individually:
Code:
int i; /* Use unsigned char instead of int if you program for the NES
and your array is less than 255 entries. */
for (i = 0; i < 5; ++i)
array[i] = 1;
But even if you
do initialize each item individually (because each item gets another value that cannot be calculated somehow), the best method is
Code:
array[0] = 25;
array[1] = 7;
array[2] = 130;
etc. and not
Code:
unsigned char *pointer = array;
*pointer = 25;
++pointer;
*pointer = 7;
++pointer;
*pointer = 130;
++pointer;
An additional suggestion (at least for NES programming. It isn't necessary in PC programs): If your arrays are never larger than 255 bytes, write your own CopyData and FillData function in Assembly where the size parameter is of type byte instead of size_t. Makes the function smaller and faster than the official memcpy and memset functions.
Zepper wrote:
Code:
buffer[index] = data;
//OR...
*(buffer+index) = data;
The compiler shouldn't make a difference here, but the typical style in C is to access array entires with the brackets, not by adding the index to the pointer address.
At first glance, the latter part looks like you're doing some hacky pointer tricks while the brackets immediately show that you simply want to access an array item in the regular way.