I was trying to create a pseudo super struct to print array of structs. My basic structures are as follows.
/* Type 10 Count */ typedef struct _T10CNT { int _cnt[20]; } T10CNT; ... /* Type 20 Count */ typedef struct _T20CNT { long _cnt[20]; } T20CNT; ...
I created the below struct to print the array of above mentioned structures. I got dereferencing void pointer error while compiling the below code snippet.
typedef struct _CMNCNT { long _cnt[3]; } CMNCNT; static int printCommonStatistics(void *cmncntin, int cmncnt_nelem, int cmncnt_elmsize) { int ii; for(ii=0; ii<cmncnt_nelem; ii++) { CMNCNT *cmncnt = (CMNCNT *)&cmncntin[ii*cmncnt_elmsize]; fprintf(stout,'STATISTICS_INP: %d\n',cmncnt->_cnt[0]); fprintf(stout,'STATISTICS_OUT: %d\n',cmncnt->_cnt[1]); fprintf(stout,'STATISTICS_ERR: %d\n',cmncnt->_cnt[2]); } return SUCCESS; } T10CNT struct_array[10]; ... printCommonStatistics(struct_array, NELEM(struct_array), sizeof(struct_array[0]); ...
My intention is to have a common function to print all the arrays. Please let me know the correct way of using it.
Appreciate the help in advance.
Edit: The parameter name is changed to cmncntin from cmncnt. Sorry it was typo error.
Thanks, Mathew Liju
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
intand T20CNT contains 20long. On modern 64-bit machines – other than under Win64 –sizeof(long) != sizeof(int). Therefore, the code inside your printing function should be distinguishing between dereferencingintarrays andlongarrays. 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 3longvalues; 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
intandlongas synonyms; but you must get out of the habit of thinking that they are synonyms, though. Thevoid *argument is the correct way to say ‘this function takes a pointer of indeterminate type’. However, inside the function, you need to convert from avoid *to a specific type before you do indexing.(I like the idea of a file stream called
stouttoo. 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…
const void *into aconst char *; this allows you to do byte-size operations on the address. In the days before Standard C,char *was used in place ofvoid *as the universal addressing mechanism.ith element of the array of objects of sizeelemsize.From there, the code is easy enough. Note that since the CMNCNT structure contains
longvalue, I used%ldto tell the truth tofprintf().Since you aren’t about to modify the data in this function, it is not a bad idea to use the
constqualifier 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 ofint‘ and ‘array oflong‘ structure types.