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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:29:32+00:00 2026-06-18T11:29:32+00:00

how can i define a general map operation on an array in C? ideally

  • 0

how can i define a general map operation on an array in C?

ideally I want something like python’s map(function,array) ~~ but as a macro. I believe this would be something like C++’s std::transform, but would be in C, and not use iterators..

(this would be unary operation)
I was thinking something like:

template <class T*, class U*,size_t N>
T* map(T (*func)(U), U* arr,size_t N)
{
   T* tmp = (T*)malloc(sizeof(T) * N);
   size_t i;
   for(i=0;i<N;i++)
   {
       *(tmp+i) = *func(*(arr+i));
   }
}

… but of course templates are in C++..
so how can I 1) do the latter and 2) if you could, could you fix the above code snippet.

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-06-18T11:29:33+00:00Added an answer on June 18, 2026 at 11:29 am

    For a template like this, there is a fairly straightforward translation to macros; the major syntactic wrinkle is that you can’t return the result array, the variable to write it to has to be another parameter.

    #define map(func_, input_, output_, type_, n_) do { \
        output_ = xmalloc(sizeof(type_) * (n_));        \
        size_t i_;                                      \
        for (i_ = 0; i_ < (n_); i_++)                   \
            output_[i_] = func_(input_[i_]);            \
      } while (0)
    

    This is not as type-unsafe as it looks, provided you pay attention to your compiler warnings. However, it is not particularly safe if any of the actual arguments to a use of this macro isn’t a simple identifier. Most importantly, catastrophic things will happen if any of the actual arguments has side effects.

    This can be cured, as can the inability to return the result array, but only if you’re willing to use GNU extensions…

    #define gnumap(func_, input_, type_, n_) ({                    \
        __typeof(func_)   func__   = (func_);                      \
        __typeof(input_)  input__  = (input_),                     \
                          output__ = xmalloc(sizeof(type_) * n__); \
        __typeof(n_)      n__      = (n_),                         \
                          i__;                                     \
        for (i__ = 0; i__ < n__; i__++)                            \
            output__[i__] = func__(input__[i__]);                  \
        /* return */ output__;                                     \
      })
    

    Would I do either of these in real life? Probably not, but sometimes it really is the least bad available option. Think of it as one step shy of rewriting that critical inner loop in assembly language.

    (xmalloc, in case you’re unfamiliar with it, is the conventional name for a user-written wrapper around malloc that either succeeds or crashes the entire program. I use it here to dodge the question of how to cope with malloc failing.)

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

Sidebar

Related Questions

I know I can define default parameters in python, but can I do so
How can I define a general function without the exact expression in Mathematica? For
I feel like there's something about grids that I'm missing, but I can't find
I'd like to be able to define a function that takes an interface, but
In core.php I can define Configure::write('Routing.admin', 'admin'); and /admin/controller/index will work. but if I
I know '+', '?' and '*'. But what if I want something repeats itself
I would like to define a completely generic mapping in c++ where I can
I think I got what a continuations is (in general), but I can't understand
I can define default value in domain by this way : class ProcessingPriority {
I can define church numerals fairly easy using scheme: > (define f (lambda (x)

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.