OK I'm pretty much a C programming newbie (or to be more accurate and 'oldbie' who has forgotten nearly all he ever knew) and I'm trying to get the handle on some subtle differences between two sets of code that do the same (or similar) thing.
This is a macro from Program A
Code:
/* Wipe an array of type T[N], at location P, and return P */
#define C_WIPE(P, N, T) \
(memset((P), 0, (N) * sizeof(T)))
And this is an example of it in use
Code:
/* Clear the "aux" array */
(void)C_WIPE(aux, MAX_DEPTH, s16b);
Now the same area from the (distant relation) Program B
Code:
/* Wipe an array of type T[N], at location P, and return P */
#define C_WIPE(P,N,T) \
(T*)(memset((char*)(P),0,(N) * sizeof(T)))
and it in use
Code:
/* Clear the "aux" array */
C_WIPE(&aux, MAX_DEPTH, s16b);
The code in Program B was probably previously used in Program A before being updated, so I might assume that A is more 'correct', but it is not clear to me why (if so).
Some things that I did think
& (address of) and * (pointer to)
look like they'd cancel each other out so I'm not sure why version B goes round the houses like it seems to do. Unless it's to turn it into a pointer to char type in particular.
B takes the trouble to return it as 'pointer to type T' but in practice the return value is not looked at in any code used in the program (sometimes it is cast to (void), sometimes not).
Any comments?