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

  • Home
  • SEARCH
  • 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 7599749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:44:18+00:00 2026-05-30T22:44:18+00:00

Programming in C, are there any techniques one can use to avoid (or at

  • 0

Programming in C, are there any techniques one can use to avoid (or at least minimize the likelihood of) casting a void * to the wrong pointer type by mistake? I’m working on a program that parses several different types of CSV datafiles and stores the fields into specific data structures for processing. For example, the records of one of the data files is stored in a hash table; data from another file is stored in a directed graph.

I’d like to create one main parsing function that reads in a record of fields from the file and passes each record to a function that tokenizes the fields and stores them in the appropriate data type. Instead of creating separate parsing/tokenizing functions for each file-type, I wanted to create a generic function to do this. In my design, the calling function would pass a record of fields, a function pointer to the tokenizer applicable to the data-file, and a void* pointing to a node of the destination data structure applicable to the data-file.

What I want to know is whether there is any way to ensure that the user does not mistakenly call the parsing function with a mismatched tokenizer / data structure. (By using the pointer to void, the compiler is certainly helpless here.) Or, if there are no such techniques, are there any effective exception-handling methods to catch this error and prevent major problems (e.g., sigfaults)?

I’d like the code to be as portable as possible.

Any thoughts? I’m not married to this algorithm, if someone has a better idea I’m open to it.

  • 1 1 Answer
  • 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. Editorial Team
    Editorial Team
    2026-05-30T22:44:19+00:00Added an answer on May 30, 2026 at 10:44 pm

    One possibility is to create a set of very thin wrapper functions that abstract away the type-safety issues:

    void foo_generic(FieldRecords *records, Parser *parser, void *dest);
    
    ...
    
    void foo_A(FieldRecords *records, A *dest) { foo_generic(records, &parse_A, dest); }
    void foo_B(FieldRecords *records, B *dest) { foo_generic(records, &parse_B, dest); }
    void foo_C(FieldRecords *records, C *dest) { foo_generic(records, &parse_C, dest); }
    

    Now all the potentials for typos are restricted to a single location, where mistakes should be easier to find due to the symmetry.

    If you’re feeling especially naughty, you could use a macro to simplify the generation of these wrapper functions:

    #define FOO(T) void foo_##T(FieldRecords *records, T *dest) { \
                       foo_generic(records, &parse_##T, dest); \
                   }
    
    FOO(A)
    FOO(B)
    FOO(C)
    

    This minimises the probability of typos, but increases the chance of confusing the debugger/IDE!

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

Sidebar

Related Questions

Is there any really low level programming language that can get access the memory
Is there any programming interface or CLI for changing network or file sharing settings
Is there any programming language (or type system) in which you could express the
Update: This question is a duplicate of Are there any programming languages targeting PHP,
Are there any open source or commercial web programming language that function much like
Is there any website which provides core java based programming challenges that tests programming
Is there any cheat sheet available for Android programming. Most commonly used classes with
Are there any PIC microcontroller programmers here? I'm learning some PIC microcontroller programming using
I am starting to learn the Clojure programming language. Are there any recommendations for
How can I write to any block on my HDD using the C programming

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.