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 6389903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:27:44+00:00 2026-05-25T03:27:44+00:00

I have to write code in C where the user has to have flexibility

  • 0

I have to write code in C where the user has to have flexibility in choosing any existing DB, write to files, or implement their own storage mechanism. I need wrapper functions that redirect to the right functions corresponding to the storage mechanism selected at runtime or compile time. Say my storage options are FLATFILE and SQLDB and my wrapper function is insert(value). So, if I select FLATFILE as my storage, when I call the wrapper function insert(value), it should in turn call the function that writes to a file. If I choose a SQLDB, insert(value) should call the function that insert the values in the data base.

I know I can somehow use a structure of function pointers to do wrapper functions, but I have no idea how.

Does anyone know of any docs, links, examples, etc I could refer to, to understand and implement something like this? Any pointers will be appreciated. Thanks!

Thanks!

  • 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-25T03:27:44+00:00Added an answer on May 25, 2026 at 3:27 am

    You can use a simple version such as:

    struct backend {
       int (*insert)(...);
       int (*remove)(...);
       ...
    };
    
    static struct backend db_backend = { db_insert, db_remove, ... };
    static struct backend other_backend = { other_insert, other_remove, ... };
    
    const struct backend *get_backend(enum backend_type type)
    {
      switch (type)
      {
         case DB_BACKEND:
           return &db_backend;
         case DB_OTHER:
           return &db_other;
         ...
      }
    }
    

    All of the above can be hidden inside a C file, with get_backend and the enumeration being public. Then you can use it like this:

    struct backend *b = get_backend(DB_BACKEND);
    b->insert(...);
    b->remove(...);
    

    Many details are missing, of course (many people like using typedef, for example). This is a basic setup, you can also create wrapper functions if you don’t like the b->insert(...) syntax or if you want to set the back end once and then use insert() and remove() in the code. This is also useful if you already have some code that calls insert() directly and you want to direct the call to the right back end.

    If you want a more elaborate solution, have a look at http://www.cs.rit.edu/~ats/books/ooc.pdf. You don’t have to implement every last detail from it, but it can give you a few ideas.

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

Sidebar

Related Questions

I have an application that allows users to write their own code in a
I'm an avid vim user and have started to write some SQL code recently.
I'm planning to write a game, where the user has to write his own
Sometimes I have to write code that alternates between doing things and checking for
What are your thoughts about them? Sometimes I have to write unmanaged code at
So far I have managed to write some code that should print the source
For a school assignment I have to write x86 assembly code, except I can't
I'm sure I'm going to have to write supporting javascript code to do this.
I have a quad core machine and would like to write some code to
Question simple and quick: I have started to use Netbeans to write some code

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.