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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:11:00+00:00 2026-05-27T13:11:00+00:00

Being able to define an array e.g. int a[] = {1,2,3}; is very convenient,

  • 0

Being able to define an array e.g.

int a[] = {1,2,3};

is very convenient, however, the array a is an r-value so I can’t subsequently change the values in a, e.g.

a[] = {4,5,6};

The context for wanting to do this is writing a bunch of unit tests where I am feeding in arrays to functions and testing the outputs. I’m running tests on the same function with different inputs and would like to avoid having to have unique names for my input arrays, e.g. I’m having to do this:

int test1_a[] = {1,2,3};
/* calls to functions */

int test2_a[] = {4,5,6};
/* calls to functions */

Also, if I want to pass a pointer to an array into a function I have to 1st cast it like this:

int a[] = {1,2,3};
int *b = a;

my_func(&b);

passing a pointer to an r-value like this doesn’t work:

my_func(&a);

My question is whether there is any other way to easily initialise an array of values without suffering from these limitations? (particularly with a view to making it easy to write many similar unit tests without each test having a unique set of array names)

  • 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-27T13:11:00+00:00Added an answer on May 27, 2026 at 1:11 pm

    If you already have the values you want to pass to the functions, why not use a multi-dimensional array?

    int a[][] = {
        { 1, 2, 3 },
        { 4, 5, 6 }
    }
    
    for (int i = 0; i < 2; i++)
    {
        /* Call functions with 'a[i]' as argument */
    }
    

    Also, if the functions you call expect an array, and you have a e.g. int a[] = {...}; int *b = a;, then don’t call them with &b. Using &b passes the address of the pointer, not what it points to.

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

Sidebar

Related Questions

Other being able to sanity check values in a setter is there a more
How can I prevent the user from being able to resize an image in
Why does it complain about it not being able to convert a string array
Not being able to find the answer, and also not being able to do
I seem to remember being able to print out (or locate) the specific switches
Edit2: After finally being able to profile the two against each other, it appears
I love being able to modify the arguments the get sent to a function,
I remember years ago being able to download and install the forums software used
I'm not being able to make this line work with Tk import os while(1):
How do I accomplish being able to paste data from Excel into a DataGridView

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.