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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:01:20+00:00 2026-05-15T17:01:20+00:00

I perform some calculations, based on the result, I would like to either use

  • 0

I perform some calculations, based on the result, I would like to either use a short int or int for some type of data for the remaining program. Can (/How can) this be done sensibly in C or C++? I don’t really care about the amount of memory used (i.e., 2 or 4 bytes), my primary aim is to access generic arrays as if they contained data of this type. I would like to avoid code such as the following:

char s[128];
if (result of preliminary calculations was A)
  *((int*) s) = 50;
else
  *((short int*) s) = 50;

to set the first 4 or 2 bytes of s. A conditional global typedef would be ideal:

if (result of preliminary calculations was A)
  typedef int mytype;
else 
  typedef short int mytype;

I am not that familiar with C++ class templates (yet). Do they apply to my problem? Would I have to change the declarations throughout my program (to myclass< > and myclass< >*)?

Many thanks!

Frank

Edit: The values may not always be aligned. I.e, a int can start at position 21. Thanks for the answers.

  • 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-15T17:01:21+00:00Added an answer on May 15, 2026 at 5:01 pm

    For plain C, you could do this using function pointers:

    static union { s_int[32]; s_short[64]; s_char[128]; } s;
    
    static void set_s_int(int i, int n)
    {
        s.s_int[i] = n;
    }
    
    static int get_s_int(int i)
    {
        return s.s_int[i];
    }
    
    static void set_s_short(int i, int n)
    {
        s.s_short[i] = n;
    }
    
    static int get_s_short(int i)
    {
        return s.s_short[i];
    }
    
    static void (*set_s)(int, int);
    static int (*get_s)(int);
    

    Set them once based on the preliminary calculations:

    if (result of preliminary calculations was A)
    {
        set_s = set_s_int;
        get_s = get_s_int;
    }
    else
    {
        set_s = set_s_short;
        get_s = get_s_short;
    }
    

    Then just use the function pointers in the rest of the program:

    set_s(0, 50);   /* Set entry 0 in array to 50 */
    

    Your file writing function can directly reference s or s.s_char depending on how it works.

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

Sidebar

Related Questions

I would like to be able to perform some logic in the table.modifiedField method
I would like to post-process my app.config file and perform some token replacements after
I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm
I need to perform some action when my application receives focus. I've tried hooking
I want to perform some empirical trade-off's to assess the performance of applications written
I am trying to perform some unit testing on the iphone but for some
What is the right way to perform some static finallization? There is no static
I have a LinkButton that has to postback to perform some logic. Once it
How should widgets in GWT inform other widgets to refresh themselfs or perform some
I want to perform cascade delete for some tables in my database, but I'm

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.