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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:56:32+00:00 2026-05-13T19:56:32+00:00

I am working on small parser and equation solver in C, part of this

  • 0

I am working on small parser and “equation solver” in C, part of this process is to do arithmetical operations on tokens.
Each token contains void* pointer to numerical data, and enum, which defines type of data.

This is example of the function, which creates new token by adding two others tokens. In order to do so, I need to

  1. check type
  2. cast
  3. do operation
  4. create new token from result

a

Token* _CreateTokenByAddition(Token* arg1, Token* arg2){
    Token *tokenResult;
    if ((arg1->_type == tk_IntData) && (arg2->_type == tk_IntData)){

        int* intResult = malloc(sizeof(int));
        *intResult = *(int*)arg1->_data + *(int*)arg2->_data;

        tokenResult = CreateTokenFromValue(intResult, tk_IntData);
    }else
    if ((arg1->_type == tk_IntData) && (arg2->_type == tk_FloatData)){

        float* intResult = malloc(sizeof(float));
        *intResult = *(int*)arg1->_data + *(float*)arg2->_data;
        tokenResult = CreateTokenFromValue(intResult, tk_FloatData);
    }else
    if ((arg1->_type == tk_FloatData) && (arg2->_type == tk_IntData)){

        float* intResult = malloc(sizeof(float));
        *intResult = *(float*)arg1->_data + *(int*)arg2->_data;
        tokenResult = CreateTokenFromValue(intResult, tk_FloatData);
    }
    else
    if ((arg1->_type == tk_FloatData) && (arg2->_type == tk_FloatData)){

        float* intResult = malloc(sizeof(float));
        *intResult = *(float*)arg1->_data + *(float*)arg2->_data;
        tokenResult = CreateTokenFromValue(intResult, tk_FloatData);
    }
    return tokenResult;
}

I have almost identical functions for -, *, /. And I will probably need to create more.

Questions are:
how I can create one generic function which will support all simple operations such as +-*/ ?
I dont want to put this function in macro and then duplicate it 4 times by replacing mathematical operand.
Any way how I can simplify data type checks and casts from void pointers?

Any way I can make this code, better ?

Assumption: I dont have any non numerical data types (eg strings)

Thanks

Cool, thanks a lot for these replies, I see what you mean by function pointers.
I am going to think about it and use one of these methods. 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-05-13T19:56:32+00:00Added an answer on May 13, 2026 at 7:56 pm

    Short answer: c provides no syntaxtical help for that at all.

    Good news: You can support polymorphism in c by using function pointers. There are many questions explaining how to do that already present on Stack Overflow. In a minute I’ll edit in a link or two…

    Not liking the answer that I found for this use, so here goes.

    For each operation, write a set of functions that take all legal combinations of types. Each one does exactly one combination, so it is easy. Then construct a table of function pointers like this

    typedef Token* (*mathfuncptr)(void *, void *);
    
    Token* _Add_IntInt(void *, void *);
    Token* _Add_IntFloat(void *, void *);
    Token* _Add_FloatInt(void *, void *);
    /* ... */
    
    mathfuncptr Add_table[AddTypeCount][AddTypeCount] = {
      {_Add_IntInt,   _Add_IntFloat},
      {_Add_FloatInt, _Add_FloatFloat}
    };
    mathfuncptr Mul_table[MultTypeCount][MultTypeCount] = { /* ... */
    

    And now your general add function determines the two types it has and calls the right function by indexing into the table.

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

Sidebar

Related Questions

I'm working on a Boost Spirit 2.0 based parser for a small subset of
I'm a LAMP guy and ended up working this small news module for an
I'm currently working on a small console application. This application is based on a
As part of a personal project I am working on I took a small
As part of a small project I'm working on, I need to be able
I'm working on a small templating engine, and I'm using DOMDocument to parse the
Iam working on small booking room system. In my solution I have a Reservation
is there any small working program for recieving from and sending data to client
I am working on small python payroll project where you enter employee name, wage,
I've been quite used to working on small projects which I coded with 1,000

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.