Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88393

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:33:10+00:00 2026-05-10T22:33:10+00:00

I was trying to create a pseudo super struct to print array of structs.

  • 0

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

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:33:11+00:00Added an answer on May 10, 2026 at 10:33 pm

    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 ith 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Using git show To complete your own answer, the syntax… May 11, 2026 at 7:26 am
  • added an answer How about this... CREATE PROC TagMe(@TagName VARCHAR(100),@TaggedRecordId INT) AS DECLARE… May 11, 2026 at 7:26 am
  • added an answer I use this pattern a lot: Select Last_Name, First_Name, Mid_Name,… May 11, 2026 at 7:26 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.