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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:30:54+00:00 2026-06-02T15:30:54+00:00

I was supposed to write C implementation of C++ STL container map (associative array)

  • 0

I was supposed to write C implementation of C++ STL container map (associative array) by hashtable (array of pointers to linked lists of data) and some supporting functions, i.e. inserting element, deleting table… I’ve successfully written all of these, except for one, which is foreach(table, function_ptr) function, which calls the passed function for all data in the table (printing contents…).

I’m a little stuck here because I can’t figure out what parameters should be passed to function_ptr, so it would be universal. As for now, I don’t think it’s possible.

If I would like just to pass a pointer for example to printf, it would be easy, prototype of foreach would look like this

foreach(table_t *t, int (*function_ptr)(const char *fmt, ...))

and I would just call it for every data node like this

function_ptr("%s, %d\n", node.key, node.data)

but if I use this and change my mind someday that I would like to pass my own function, I would have to change the code of caller-function and the foreach function as well.

Is there any easy way to do something like this?

  • 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-06-02T15:30:57+00:00Added an answer on June 2, 2026 at 3:30 pm

    The conventional way to specify “any argument type” is by using a void * like this:

    foreach(table_t *t, int (*function_ptr)(void *p))
    

    Then you can pass the address of each argument (which may be a complex data type, like a structure), and the function can cast it back to the appropriate type:

    struct {
      int x;
      int y;
    } numbers;
    
    // Sums the numbers in a structure
    int sum(void *p) {
      numbers *n = (numbers *) p;  // Cast back to the correct type
      return n->x + n->y;
    }
    
    // Counts the number of 'a' chars in a string
    int numberOfA(void *p) {
      char *s = (char *) p;
      int num = 0;
      while (s != NULL && *s != '\0') {
        if (*s == 'a') {
          ++num;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am supposed to write a program that gets some PNG images from the
I am supposed to write a program in JavaScript to find all the anagrams
Given the following code (it's supposed to write helloworld in a helloworld file, and
When using a perspective matrix in a vertex shader am I supposed to write
Java is supposed to be write once, run anywhere and it really can be,
the code that i am trying to write is supposed to read text from
This code is supposed to read postfix problems from a file and and write
I need to write a java script. This is supposed to validate if the
I've got some simple XML-handling code that's supposed to locate a child node of
I have a global object holding several c++ std::map objects. This object is supposed

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.