3

I will really appreciate and remain grateful to him/her who can help me for solving my problem in CLR (Common Language Runtime). It is similar like C/C++. Generally we use the below method to assign value in array.

1. int  values[] = { 1,2,3,4,5,6,7,8,9 };
2. Looping.

In my program I need to use this assignment after declaration. Like below,

int  values[] = { 1,2,3,4,5,6,7,8,9 };
values[] = { 10,20,30,40,50,60,700,800,900 };

But it is not working.The second statement is erroneous said by compiler.I do not want to use looping. Is there any way to assign array value in second bracket after declaring the array? Please help me. Thanks.

2
  • 1
    The long and short of it is "you can't do it that way". Commented Sep 2, 2014 at 17:10
  • But I need that without looping..@MadScienceDreams Commented Sep 2, 2014 at 17:11

1 Answer 1

2

You can use standard class std::array instead. For example

#include <array>

//...


std::array<int, 9> values = { 1,2,3,4,5,6,7,8,9 };
values = { 10,20,30,40,50,60,700,800,900 };

Here is a demonstrative program

#include <iostream>
#include <array>


int main()
{
    std::array<int, 9> values = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for ( int x : values ) std::cout << x << ' ';
    std::cout << std::endl;

    values = { 10, 20, 30, 40, 50, 60, 700, 800, 900 }; 

    for ( int x : values ) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

The output is

1 2 3 4 5 6 7 8 9 
10 20 30 40 50 60 700 800 900 

Though I did no test the program in the CLR environment.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping me by thinking in a different way. Please don't forget to Upvote the question if you think.I will remain grateful to you for helping me @Vlad from Moscow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.