Pages: [1]   Go Down
Author Topic: problem returning array from function...  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 43
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to use a function to generate an array. Below is a stripped down version of my code. In the loop, the variable named "pattern" should get an array of numbers from the function generatePattern(). Thanks for any help.


Code:
int pattern_local[] = {1, 2};
int pattern[] = {3, 4};


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  pattern = generatePattern();
}



int generatePattern()
{
 int pattern_local[] = {1, 2};
 return pattern_local;
}

This errors out with:
Code:
In function 'void loop()':
error: incompatible types in assignment of 'int' to 'int [2]' In function 'int generatePattern()':

Can anyone point me in the right direction?
Logged

Keynsham, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 224
I like for loops and walks on the beach,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The array contains two integers and you cannot assign them to a single integer. so you need to choose one value in the array to assign or do some maths!

/me
Logged

"C++ : Where friends have access to your private members." - Gavin Russell Baker

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't write functions to "return an array."

Write functions that can fill out a given array.  Remember to tell the function how long the array is.

Code:
#define PATTERN_LENGTH 2

int the_pattern[PATTERN_LENGTH];

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    generatePattern(the_pattern, PATTERN_LENGTH);
}

void generatePattern(int pattern[], int length)
{
    int value = 1;
    for (int i = 0; i < length; i++)
    {
        pattern[i] = value;
        value++;
    }
}
« Last Edit: April 15, 2009, 09:01:49 am by halley » Logged

Pages: [1]   Go Up
Jump to: