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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:42:18+00:00 2026-05-23T16:42:18+00:00

I am working on certain script language. Values containing structure is struct myvar {

  • 0

I am working on certain script language. Values containing structure is

struct myvar
{
 char  name[NAMELEN];
 int   type;
 void* value;
}
type = 0  --> int* value
type = 1  --> char* value
type = 2  --> float* value

I faced some problem with arithmetic operations. It seems that I need to commit all sorts of type conversions over every single operation, that develops into writing a whole bunch of code for each of them, as in:

case 0:  // "="
 if(factor1.name)
 {
    if((factor1.type == 1) && (factor2.type==1))
    {
        free(factor1.value);
        int len = (strlen((STRING)factor2.value)+1)*sizeof(char);
        factor1.value = malloc(len);
        memcpy(factor1.value,factor2.value,len);
    }
    else if((factor1.type == 2) && (factor2.type==2))
    *(FLOAT*)factor1.value = *(FLOAT*)factor2.value;
    else if((factor1.type == 0) && (factor2.type==0))
    *(INTEGER*)factor1.value = *(INTEGER*)factor2.value;
    else if((factor1.type == 0) && (factor2.type==2))
    *(INTEGER*)factor1.value = *(FLOAT*)factor2.value;
    else if((factor1.type == 2) && (factor2.type==0))
    *(FLOAT*)factor1.value = *(INTEGER*)factor2.value;
    else
     GetNextWord("error");
 }
 break;

Is there some way to avoid this tiresome procedure? Otherwise I have no choice but to copy-paste this piece of code for each of “=”,”~”,”+”,”-“,”*”,”/”,”%”,”>”,”<“,”>=”,”<=”,”==”,”~=”,”AND”,”OR”

  • 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-23T16:42:18+00:00Added an answer on May 23, 2026 at 4:42 pm

    What about writing 3 toType functions:

    char* toType0(myvar* from)
    {
       if (from->type == 0) return (char*)(from->value);
       else if (from->type == 1) return itoa((int*)from->value);
       else...
    }
    int toType1(myvar* from)
    {
       //convert to int...
    }
    

    Then in your conversion routines you can do:

     switch (factor1.type)
     {
        case 0:
         { char* other = toType0(&factor2);
         //assign or add or whatever....
         };
         break;
         case 1:
         { int other = toType1(&factor2);
         //assign or add or whatever....
         };
         break;
         ...
      }
    
    • 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 shell script that does certain changes on a txt file
I am working on a PHP script that allows users to vote on certain
I am working on a script that develops certain strings of alphanumeric characters, separated
I have a script that generates content containing certain tokens, and I need to
I was working with a certain XML where there were no redundancies <person> <eye>
Working with PHP Xpath trying to quickly pull certain links within a html page.
I am working on a plugin for jQuery that will essentially style certain elements.
I am working on an ASP web page that handles file uploads. Only certain
A requirement for an ASP.Net 2.0 project I'm working on limits a certain field
I've got my autocomplete plugin working perfectly... except I have certain info that 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.