Pages: [1] 2   Go Down
Author Topic: Assigning array to array. Problem  (Read 804 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, so here is the code. I've done it as simply as possible because I'm trying to debug my bigger project.

Code:
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:
position 0= 1
position 1= 2
position 2= 3
position 3= 4
position 4= 5
position 5= 6
position 6= 7
position 7= 8
position 8= 9

But instead I got this

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

Do You know what's wrong?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Rob Tillaart

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int array1[]={1,2,3,4,5,6,7,8,9};  // Array of 9 elements
int 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:
int array1[]={1,2,3,4,5,6,7,8,9};  // Array of 9 elements
int 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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


the size of array 2 is unknown.

Zero-length, I would say.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This would work in Lua where arrays dynamically resize, but not in C++.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley (and lack of commas)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The sizes must match. And I guessing your doing this,
Code:
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.

« Last Edit: January 12, 2013, 06:44:00 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And I guessing your doing this,
I didn't see anything like that in OPs code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1] 2   Go Up
Jump to: