Go Down

### Topic: Assigning array to array. Problem (Read 2406 times)previous topic - next topic

#### SpyChar

##### Jan 12, 2013, 08:46 pm
Okay, so here is the code. I've done it as simply as possible because I'm trying to debug my bigger project.

Code: [Select]
`int button=12;int position1=0;int position2=0;int array1[]={1,2,3,4,5,6,7,8,9};int array2[]={};int length=0;void setup(){  pinMode(button,INPUT);  Serial.begin(9600);}void loop(){  if(digitalRead(button)==HIGH){    delay(100);    position1++;    position2++;    length++;    if (length>9) length=9;    if (position1>9) position1=9;    if (position2>9) position2=9;    array2[position2]=array1[position1]; //   <---------------- problematic part    for(int cnt=0;cnt<length;cnt++){      Serial.print("position "); Serial.print(cnt); Serial.print("= "); Serial.println(array2[cnt]);    }    Serial.println("");      }}`

What I wanted to achieve is to copy values from one array to another one by one, each one after one press of the button. I expected to get something like this

Code: [Select]
`position 0= 1position 1= 2position 2= 3position 3= 4position 4= 5position 5= 6position 6= 7position 7= 8position 8= 9`

Code: [Select]
`position 0= 9position 1= 7382position 2= 3position 3= 6076position 4= 5position 5= 2position 6= 7position 7= 8position 8= 9`

Do You know what's wrong?

#### robtillaart

#1
##### Jan 12, 2013, 08:52 pm

the size of array 2 is unknown. Give them both fixed size that's large enough. (note C arrays are zero based)

int array1[10]={1,2,3,4,5,6,7,8,9};
int array2[10]={};

give it a try.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### johnwasser

#2
##### Jan 12, 2013, 08:55 pm
Code: [Select]
`int array1[]={1,2,3,4,5,6,7,8,9};  // Array of 9 elementsint array2[]={};  // Array of 0 elements`

You can't write into element 0-8 of a zero-length array without corrupting other variables.

To fix it you need to specify how much room to set aside for the array:

Code: [Select]
`int array1[]={1,2,3,4,5,6,7,8,9};  // Array of 9 elementsint array2[sizeof array1 / sizeof array1[0]];  // Array the same size as array1`

Note: 'sizeof' is an operator that returns the size IN BYTES.  The division is there to convert from BYTES to the same size elements as array1.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### nickgammon

#3
##### Jan 12, 2013, 08:55 pm

the size of array 2 is unknown.

Zero-length, I would say.
Please post technical questions on the forum, not by personal message. Thanks!

#### nickgammon

#4
##### Jan 12, 2013, 08:56 pm
This would work in Lua where arrays dynamically resize, but not in C++.
Please post technical questions on the forum, not by personal message. Thanks!

#### SpyChar

#5
##### Jan 12, 2013, 10:35 pm
Darn. I thought this would solve the problem. Actually, what I was really thinking of was to have one fixed array, and then the other one, which would be empty. Then I wanted to be able to choose values from the first array and assign them to the second one in any order and in any amount.
And sorry for any possible language errors (and lack of commas)

#### PaulS

#6
##### Jan 12, 2013, 10:51 pm
Quote
Actually, what I was really thinking of was to have one fixed array, and then the other one, which would be empty.

A warehouse can be empty. So can a closet. Only one of them is going to be large enough to park 10 trucks in. The size of the storage space and the amount being used are two different things. The size of the storage space you had initially defined was zero. You can make it bigger, and it will still be empty.

#### SpyChar

#7
##### Jan 12, 2013, 11:26 pm
Yeah, thanks. Now I'm a little bit wiser. But I still don't know how to do this right. But hey, this is the purpose of forum - to ask questions. And by that I mean, to learn new things.

#### PaulS

#8
##### Jan 12, 2013, 11:30 pm
Quote
But I still don't know how to do this right.

Part of asking questions is asking them in a way that includes enough detail. What is it you are trying to do?

#### SpyChar

#9
##### Jan 13, 2013, 12:23 am
Ok, I'll tell You but promise you won't laugh. I'm trying to make a "music synthesizer" as I like to call it. I have one array with notes frequencies assigned to it, and now I want to be able to scroll through them using buttons, and assign them to this second array.

#### PaulS

#10
##### Jan 13, 2013, 12:31 am
Quote
Ok, I'll tell You but promise you won't laugh.

OK, I won't.

Quote
I have one array with notes frequencies assigned to it, and now I want to be able to scroll through them using buttons, and assign them to this second array.

Switches would be more useful, but, whatever works.

The second array needs to have a size associated with it. Now, that size can be a fixed/static size, or it can be a dynamic size, growing as more notes are added to it. There is, of course, a limit as to how large the array can grow to. There are a number of issues with dynamic arrays, mostly related to the fact that the Arduino hasn't got that much memory to be used for dynamic (or static) arrays.

You can minimize the amount of SRAM used by putting the first array in PROGMEM, since that array won't change. Getting that out of SRAM leaves more room for your static (or dynamic) second array.

What do you intend to do with this second array when it is populated?

#### HazardsMind

#11
##### Jan 13, 2013, 12:33 amLast Edit: Jan 13, 2013, 12:44 am by HazardsMind Reason: 1
The sizes must match. And I guessing your doing this,
Code: [Select]
`Array1[x] = Array2[y] ` correct?
If your getting garbage in array2 then make it array2[]={0,0,0,0,0,0,0,0,0} to match array1.
@PaulS,
Yea that was weird, [ x ] gave me that black bullet.

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### PaulS

#12
##### Jan 13, 2013, 12:34 am
Quote
And I guessing your doing this,

I didn't see anything like that in OPs code.

#### SpyChar

#13
##### Jan 13, 2013, 12:41 am
Well, as I said that was only test code. In my main code, I'm doing it like this: array1[position1]=array2[position2] after pressing the buttons and raising position1 or position2 as needed.

edit. actually, this part of code is here too.

And what I want to do with the second array is to iterate(good word?) though it using for loop and play those values with buzzer.

#### PaulS

#14
##### Jan 13, 2013, 12:47 am
Quote
And what I want to do with the second array is to iterate(good word?) though it using for loop and play those values with buzzer.

Yes, iterate is a good word. How big are you expecting the array of notes/frequencies to be? 10? 100? 1000?

How will you transition from "record some notes" to "annoy me by endlessly repeating those notes" mode? What will happen when the Arduino is reset?