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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:04:25+00:00 2026-06-10T08:04:25+00:00

I have a some pieces of code in my project in which a structure

  • 0

I have a some pieces of code in my project in which a structure z is being used. This structure declaration changes as per a condition flag being set in my system. I want the structure declaration to be dynamic.

if condition flag is 0 then,

struct x z;

if condition flag is 1 then,

struct y z;

but the operation to be performed on z would be the same. Currently this is handled by declaring both the structure :

struct x z_x;
struct y z_y;

then as per the condition flag use the appropriate structure in the code. This method doesn’t seem to be optimal. Any suggestions on handling this type of code ?

  • 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-10T08:04:27+00:00Added an answer on June 10, 2026 at 8:04 am

    As the flag is run-time, you cannot avoid checking this flag all the time. At least, I can’t think of such way.

    But you still can reduce the memory and the code. I’d do it like this:


    To reduce the memory usage:

    struct z_x* = NULL;
    struct z_y* = NULL;
    
    //...
    
    if( flag )
    {
        z_x = malloc( sizeof( struct x ) );
    }
    else
    {
        z_y = malloc( sizeof( struct y ) );
    }
    

    To reduce the code, instead of writing all the time things like:

    if( flag )
    {
        // do operations_N with z_x
    }
    else
    {
        // do operations_N with z_y
    }
    

    I’d write:

    operations_N( z_x, z_y, flag );
    

    where

    void operations_N( struct x* z_x, struct y* z_y, flag )
    {
        if( flag )
        {
            // do operations_N with z_x
        }
        else
        {
            // do operations_N with z_y
        }
    }
    

    of course, operations_1 and flag should have some good and logical names.

    BUT this could lead do some problems, so just be careful, if you decide to use this approach.

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

Sidebar

Related Questions

I have a Class Library project which houses some shared code between other projects
I have this piece of code which I am using to update some values
I have got a piece of code, that should countdown some number (in this
I have written some extension modules for eJabberd most of which pass pieces of
In the past I had quite some reusable code in my project which I
I have added some debugging code to my app which I want to call
I have a Login.aspx page, and I wanna incude some piece of code in
If have a piece of code that gets some data from a sql database
I have the following piece of code that takes in some words, stores them
I am reading some book and I have encountered a piece of code 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.