I think your design is going to fail, but I am also unconvinced that the other answers I see fully deal with the deeper reasons why.
It appears that you are trying to use C to deal with generic types, something that always gets to be hairy. You can do it, if you are careful, but it isn't easy, and in this case, I doubt if it would be worthwhile.
Deeper Reason: Let's assume we get past the mere syntactic (or barely more than syntactic) issues. Your code shows that T10CNT contains 20 int
and T20CNT contains 20 long
. On modern 64-bit machines - other than under Win64 - sizeof(long) != sizeof(int)
. Therefore, the code inside your printing function should be distinguishing between dereferencing int
arrays and long
arrays. In C++, there's a rule that you should not try to treat arrays polymorphically, and this sort of thing is why. The CMNCNT type contains 3 long
values; different from both the T10CNT and T20CNT structures in number, though the base type of the array matches T20CNT.
Style Recommendation: I strongly recommend avoiding leading underscores on names. In general, names beginning with underscore are reserved for the implementation to use, and to use as macros. Macros have no respect for scope; if the implementation defines a macro _cnt it would wreck your code. There are nuances to what names are reserved; I'm not about to go into those nuances. It is much simpler to think 'names starting with underscore are reserved', and it will steer you clear of trouble.
Style Suggestion: Your print function returns success unconditionally. That is not sensible; your function should return nothing, so that the caller does not have to test for success or failure (since it can never fail). A careful coder who observes that the function returns a status will always test the return status, and have error handling code. That code will never be executed, so it is dead, but it is hard for anyone (or the compiler) to determine that.
Surface Fix: Temporarily, we can assume that you can treat int
and long
as synonyms; but you must get out of the habit of thinking that they are synonyms, though. The void *
argument is the correct way to say "this function takes a pointer of indeterminate type". However, inside the function, you need to convert from a void *
to a specific type before you do indexing.
typedef struct _CMNCNT
{
long count[3];
} CMNCNT;
static void printCommonStatistics(const void *data, size_t nelem, size_t elemsize)
{
int i;
for (i = 0; i < nelem; i++)
{
const CMNCNT *cmncnt = (const CMNCNT *)((const char *)data + (i * elemsize));
fprintf(stdout,"STATISTICS_INP: %ld\n", cmncnt->count[0]);
fprintf(stdout,"STATISTICS_OUT: %ld\n", cmncnt->count[1]);
fprintf(stdout,"STATISTICS_ERR: %ld\n", cmncnt->count[2]);
}
}
(I like the idea of a file stream called stout
too. Suggestion: use cut'n'paste on real source code--it is safer! I'm generally use "sed 's/^/ /' file.c
" to prepare code for cut'n'paste into an SO answer.)
What does that cast line do? I'm glad you asked...
- The first operation is to convert the
const void *
into a const char *
; this allows you to do byte-size operations on the address. In the days before Standard C, char *
was used in place of void *
as the universal addressing mechanism.
- The next operation adds the correct number of bytes to get to the start of the
i
th element of the array of objects of size elemsize
.
- The second cast then tells the compiler "trust me - I know what I'm doing" and "treat this address as the address of a CMNCNT structure".
From there, the code is easy enough. Note that since the CMNCNT structure contains long
value, I used %ld
to tell the truth to fprintf()
.
Since you aren't about to modify the data in this function, it is not a bad idea to use the const
qualifier as I did.
Note that if you are going to be faithful to sizeof(long) != sizeof(int)
, then you need two separate blocks of code (I'd suggest separate functions) to deal with the 'array of int
' and 'array of long
' structure types.