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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:40:48+00:00 2026-06-07T09:40:48+00:00

When I learn delegate of C#, my book said that delegate same with function

  • 0

When I learn delegate of C#, my book said that delegate same with function pointer in C, but more safer. After I read that line, I think : Ah, so C Compiler will not check prototype where the function pointer points to. And I’m totally wrong.

int add(int a, int b){ return a + b; }
float add_f(float a, float b){ return a + b; }

int (*f)(int,int);
f = add; // no compile-error
f = add_f; //compile-error

So, Please tell me why, and give me some examples to prove that C function pointer is unsafe when compare to delegate in C#, please.

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-07T09:40:50+00:00Added an answer on June 7, 2026 at 9:40 am

    In C++, function pointers are type-safe. You have to do something spectacularly stupid to break them (generally involving casting, unions, memcpy, or similar; it can’t happen by accident).

    In C, though, function types aren’t nearly as strict. You can do things like this:

    int add(int a, int b){ return a + b; }
    int add_f(float a, float b){ return a + b; }
    
    int (*f)(); // empty argument list does NOT mean zero arguments.
                // for zero arguments, say int (*)(void)
    f = &add;
    f = &add_f;
    

    http://ideone.com/Y6mCc


    I was asked for an example of spectacularly stupid. Here goes:

    #include <iostream>
    
    int add(int a, int b){ return a + b; }
    int add_f(float a, float b){ return a + b; }
    
    union {
        int (*f1)(int, int);
        int (*f2)(float, float);
    } fp;
    
    int main(void)
    {
        fp.f1 = &add;
        std::cout << "Expected (1): " << add(1, 3) << std::endl;
        std::cout << "Actual   (1): " << (fp.f1)(1, 3) << std::endl;
    
        std::cout << "Expected (2): " << add(1.0f, 3.0f) << std::endl;
        std::cout << "Actual   (2): " << (fp.f2)(1.0f, 3.0f) << std::endl;
    
        fp.f2 = &add_f;
        std::cout << "Expected (3): " << add_f(1.0f, 3.0f) << std::endl;
        std::cout << "Actual   (3): " << (fp.f2)(1.0f, 3.0f) << std::endl;
    
        std::cout << "Expected (4): " << add_f(1, 3) << std::endl;
        std::cout << "Actual   (4): " << (fp.f1)(1, 3) << std::endl;
    
        return 0;
    }
    

    Note that this foolishness is equally possible in C# with Marshal.Copy.

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

Sidebar

Related Questions

I've got a PHP background, but I'm beginning to learn Objective-C, so that I
I tried to learn on my own, but soon started to realize that by
UPDATE I should learn to read error messages more carefully. I forgot to change
I'm trying to learn delegate and not sure what I do wrong. So I
im trying to learn delegates and events in c#, i understand that an event
I learn best by taking apart something that already does something and figuring out
To learn more about Eclipse plugin development, it would be cool if I could
To learn backbone Im creating a Twitter like app. So you know that Twitter
In a seemingly never ending effort to learn more about iphone development, I have
I am trying to learn CorePlot. I found a code example from codejunkster that

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.