How do i write values to a array ?

Dear supporter

How do I write values to a array ?

I have tried to declare the array

 int VAL[] = { 2, 7, 4, 6, 5, 3 };

it works quite well, but I would like to set the array up, over and over again and this line, cant be repeated without errors Then i tried this

 VAL[6] = {1,0,0,0,1,0};

but this do not work to well

Best regards Steen

If you want to initialise an array with values:

int val[] = { 1,2,3,4,5 } ;

will create an array with 5 elements.

If you want to create an array with no values in it ( defaults to zero )

int val[5] ;

If you want to assign a value to an element of the array later.

int j=4 ;   // must be a value between 0 and 4 for a 5 element array
val[j] = 27 ;   // puts the value 27 into the 5th element of the array.

There is no straightforward method in C or C++ to assign new values to all the elements of an array at once.

Since you seem to have no idea what you are doing, I suggest that you buy, borrow or steal an elementary book on C/C++ programming, and read it, or a suitable online resource. Randomly trying different nonsense isn't really going to work. Saves your time and ours.

Hi michinyon

thank you for your first reply, have tried to assign values to the array, one at the time as you suggest.

What i needed was a way to write to all 6 variables in the array, in one line of code.

And for your second reply. which 2 books on C++ would you recommend for a newbee. And i am a little surprised, I thought arduino was made, to make things easy and, that we on the forum was mend to help each other using it. I have read 3 tutorials on arrays for arduino, but non of them mentioned how to write to all sections of an array, though it must be possible

What i needed was a way to write to all 6 variables in the array, in one line of code.

You can't, so you need to get over it.

I have read 3 tutorials on arrays for arduino, but non of them mentioned how to write to all sections of an array,

Again, you can't

though it must be possible

What makes you think that ?

You could write a function to do it using a for loop so that it looked like one line of code when the function was called.

Personally I blame K&R but I don't think that they subscribe to this forum.

Thanks again

I am very sorry, i just sought it was so obvious

that when you could do ----- int val[] = { 1,2,3,4,5 } ;

there would be a way to do almost the same insight the main loop

Best regards Steen

int arr[] = {1, 2, 3, 4, 5};
void setup(){
    Serial.begin(115200);
    memcpy(arr, "\6\0\7\0\b\0\t\0\n\0", 10);
    for(int i = 0; i<5; i++) Serial.println(arr[i]);
}//setup()

void loop(){}//loop()

sth1709:
What i needed was a way to write to all 6 variables in the array, in one
line of code.

//
Not really possible but using loop, you can change array in few lines of code.

int VAL[] = { 2, 7, 4, 6, 5, 3 };
for(int i=0;i<6;i++){
VAL[i] = i; //Modify array and put numbers from 0 to 5 in them
}

instead of doing

VAL[0] =0;
VAL[1] =1;
VAL[2] =2;
VAL[3] =3;
VAL[4] =4;
VAL[5] =5;

To show the values in few lines, you can use loop again

for(int i=0;i<6;i++){
Serial.print(VAL[i]); //Display array values
}

instead of doing

Serial.print(VAL[0]);
Serial.print(VAL[1]);
Serial.print(VAL[2]);
Serial.print(VAL[3]);
Serial.print(VAL[4]);
Serial.print(VAL[5]);

sth1709:
And for your second reply. which 2 books on C++ would you recommend
for a newbee.

You don’t need a book unless you are in college which requires you do some homework from the book exercises. You can just learn c/c++ online and save your self some money. Go to chapter 6 to learn about array.
If you are a visual learner like me, you can use Bucky C++ youtube videos to learn which are made easy to understand.

sth1709:
And i am a little surprised, I thought arduino was made,
to make things easy and, that we on the forum was mend to help each
other using it.

Yes, Arduino is made easier with their simple development board and easy library which includes great functions such as pinMode and digitalWrite and digitalRead. The forum is NOT made as place to teach c++ to people. You come here when you are stuck on your program or when you discover a new problem or bug with the Arduino platform or software. You have to learn C++ if you want to be creating great projects with Arduino instead of a blinking light or you will end up doing copying and pasting.

Arduino founders and communities made the all in one hardware for you so that you don’t have to build your own mcu board and a programmer to program it. They also made simple libraries for you so that you will NOT have to use the bitwise operation which is a pain in the butt for newbies but it is totally up to you to take advantage of that by learning programming.

Or even easier:

int arr[] = {1, 2, 3, 4, 5};
void setup(){
    Serial.begin(115200);
    memcpy(arr, (int[]){6, 7, 8, 9, 10}, 10);
    for(int i = 0; i<5; i++) Serial.println(arr[i]);
}//setup()

void loop(){}//loop()

nilton61:
Or even easier:

int arr[] = {1, 2, 3, 4, 5};

void setup(){
    Serial.begin(115200);
    memcpy(arr, (int){6, 7, 8, 9, 10}, 10);
    for(int i = 0; i<5; i++) Serial.println(arr[i]);
}//setup()

void loop(){}//loop()

the
memcpy(arr, (int){6, 7, 8, 9, 10}, 10);

generates today a compiler error message
taking address of temporary array

so I changed it to:
memcpy(arr, (const int){6, 7, 8, 9, 10}, 10);
without actually knowing, what I am doing.

But it worked!

Thank you. Only 4 years late

sth1709: VAL[6] = {1,0,0,0,1,0};

but this do not work to well

You didn't declare a variable type for VAL. You did it in the first, why didn't you do it in the second? You should post compiler error messages. This is what I got:

sketch_jun30a:5:3: error: 'val' does not name a type
   val[6] = {1,2,3,4,5,6};
   ^
exit status 1
'val' does not name a type