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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:49:22+00:00 2026-05-28T15:49:22+00:00

Possible Duplicate: How can I simulate OO-style polymorphism in C? I am newbie to

  • 0

Possible Duplicate:
How can I simulate OO-style polymorphism in C?

I am newbie to C, C++ trying to achieve polymorphism in C (which is not supported by C) but is there any way to do it?

Where does my code go wrong? I have seen the code on this site which was bit complex for me to understand so modified it but its not working. Sorry if the question seems to be very basic or even foolish.

#include <stdio.h>

void tripple() {
    printf("in tripple");
}

void square() {
    printf("\nin sq");
}

int main() {
    void *al;
    al=&tripple;
    (*al)();
    al=&square;
    (*al)();
    return 0;
}
  • 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-28T15:49:24+00:00Added an answer on May 28, 2026 at 3:49 pm

    Frankly, you will do best to defer trying to achieve polymorphism in C until you are no longer a newbie at programming in C.

    Your code is dubious (it doesn’t compile!). That should be void (*al)(void); in your main and you should arguably include the void in the argument lists of tripple and square. You don’t need the & in front of the function names in the assignments to al, though I don’t think it does any actual harm. (Beware though; there is a difference between using an array name and the address of an array name! That is: char a[10]; char *s = a; char (*t)[10] = &a;) You should also include a newline at the end of each message in each of these functions. Newlines at the beginning of a message are often (but not always) indicative of problems. Sometimes, it is OK to omit the newline at the end of a message, but not very often.

    Any polymorphism implemented in C will use function pointers. But you should not be trying to implement polymorphism in C until you are comfortable using function pointers without attempting polymorphism. I suppose it could be said to be ‘learning to swim by jumping in at the deep end’, but you’d do better to learn a language like C++ that supports polymorphism than trying to do it in C which doesn’t really do so.

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

Sidebar

Related Questions

Possible Duplicate: How can I simulate OO-style polymorphism in C? I'm trying to better
Possible Duplicate: Can every recursion be converted into iteration? Are there problems in which
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: Can Google Maps/Places 'autocomplete' API be used via AJAX? There seems to
Possible Duplicate: Can a JavaScript object have a prototype chain, but also be a
Possible Duplicate: Can a local variable's memory be accessed outside its scope? Is there
Possible Duplicate: Can't pass mysqli connection in session in php I'm trying to store
Possible Duplicate: Can I tell Linux not to swap out a particular processes' memory?
Possible Duplicate: Can I write an iPhone application in any language other than Objective-C?
Possible Duplicate: How can I simulate an anchor click via jquery? For some reason

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.