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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:17:50+00:00 2026-06-12T21:17:50+00:00

I just came to C from C# and was looking for a way to

  • 0

I just came to C from C# and was looking for a way to define generic functions like those in C#. I came across this post but when I tried to compile it I get a bunch of errors (“`n’ undeclared here (not in a function)”, ” syntax error before “array” “, etc.)
Here’s my code:

#include<conio.h>
#include<stdlib.h>

#define MAKE_PRINTEACH(TYPE)\
void printeach_##TYPE (TYPE[n] array, int n, void(*f)(TYPE)){\
  int i;\
  for(i = 0; i < n; i++) {\
    f(array[i]);\
  }\
}

MAKE_PRINTEACH(int)
MAKE_PRINTEACH(float)

void printInt(int x)
{
     printf("got %d\n",x);
}

void printFloat(float x)
{
     printf("got %f\n",x);
}

int main()
{
    int[5] ia = {34,61,3,6,76};
    float[6] fa = {2.4,0.5,55.2,22.0,6.76,3.14159265};

    printeach_int(ia, 5, printInt);
    printeach_float(fa,6,printFloat);

    getch();
}

What am I doing wrong here?
I am using DevC++ if that makes a difference.

  • 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-12T21:17:51+00:00Added an answer on June 12, 2026 at 9:17 pm

    A correct version would look like this

    #define MAKE_PRINTEACH(TYPE)                                     \
    void printeach_##TYPE (size_t n, TYPE array[n], void(*f)(TYPE)){ \
      for(size_t i = 0; i < n; i++) {                                \
        f(array[i]);                                                 \
      }                                                              \
    }
    

    to summarize what went wrong with your version:

    • n must be declared before it is used
    • the array bounds come after the identifier
    • the semantically correct type for array sizes and things like that is size_t
    • C since C99 also has local variables for for loops.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just came across this by Chris Coyier - http://css-tricks.com/examples/CSSTabs/ Can anyone explain me, how
Was just looking at the logging docs in Python and came across funcName as
I was looking through some code from the SDL library and came across a
Just came across an issue with my Wordpress template that I can't figure out.
I just came across jLinq , a JS library to query data (e.g. arrays)
I just came across an interesting LogCat entry: 06-28 08:43:25.616 I Web Console 5075
I just came across a weird error: private bool GetBoolValue() { //Do some logic
I just came across an idea in The Structure And Interpretation of Computer Programs
I just came across few articles, while selecting a wiki for my personal site.
I just came across NetBeans site and found its PHP and Ruby IDEs. I

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.