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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:04:32+00:00 2026-06-03T11:04:32+00:00

this seems to be a simple question, but i cannot get my head around

  • 0

this seems to be a simple question, but i cannot get my head around it…

i want to set the elements of an array, based on some conditions, in the most elegant way.

this is my non-compiling pseudo code:

float*array=NULL;
switch(elements) {
  case 1: array={1}; break;
  case 2: array={7, 1}; break;
  case 3: array={3, 2, -1}; break;
  case 4: array={10, 20, 30, 40}; break;
  default:break;
}

the size of the array is limited, so i could do something like ‘float array[16];’ as well, but the problem is obviously assignment in the case-statement.

i really don’t want to do something like:

case 2: array[0]=1; array[1]=2;

my current (clumsy) implementation is:

#define ARRAYCOPY(dest, src) for(int i=0;i<sizeof(src)/sizeof(*src);i++)dest[i]=src[i]
// ...
case 2: do {float*vec={1, 2}; ARRAYCOPY(array, vec); } while(0); break;

i’m using the ARRAYCOPY define, since memcpy() doesn’t seem to work. at least doing

float*vec={1, 2}; memcpy(array, vec, sizeof(vec)/sizeof(*vec);

did not fill any values into array.

i guess there must be a nicer solution?

  • 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-03T11:04:34+00:00Added an answer on June 3, 2026 at 11:04 am

    How about:

    float *global[] = {
        NULL,
        (float[]){1},
        (float[]){1, 2},
        (float[]){7, 8, 9},
    };
    
    
    /* ... make sure `elements` is in range */
    float *array = global[elements];
    

    EDIT

    You’re obviously free to continue using the switch:

    switch(elements) {
    case 1:
        array = (float[]){1};
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems like it should be so simple, but I simply cannot get this
This seems like the most common relationship but for some reason I cannot get
This seems to be a simple question but nevertheless I haven't found an answer
This seems to be an absurdly simple question but Google and Stack Overflow searches
this seems like a simple enough question but I can't seem to find a
This seems like a simple question, but I can't find it with the Stack
This seems like a simple question, but it is difficult to search for. I
This seems like a simple question, but has proven to be difficult to find
This seems to be an embarrassingly simple question, but, after a day of reading
i think this is a simple question but I've searched around and can't seem

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.