making a function that will update and array

Hello all,
So I am not too familiar with using arrays with C
But basically what I want to do is have a function where I can pass in a value and a filled array and move every value over one, throw out the last one and put a new value in the beginning.
I do not really understand the whole * thing in c, but I have seen it around, I am not sure if I need to do it to get this function to work properly.

int brain_data[30]={82960, 21827, 39586, 34202, 47124, 69690, 9468, 47798, 6563, 47456, 42494, 66574, 92910, 19151, 23587, 155965, 45245, 33233, 23437, 22788, 168338, 4015, 20487, 44382, 8065, 85082, 5408, 147608, 109213, 109210};

int * addValue(int number , int array[]){
   for (int i=0; i<29; i++){
    int array_val;
    array_val=array[i];
    array[i+1]=array_val; 
   }
   array[0]=number;
   return array;
  
}
void setup()
{
  Serial.begin(9600);
 Serial.println(addValue(500, brain_data));
}
void loop(){
  
}

This code returns the following error:

call of overloaded 'println(int*) is ambiguous

Hi.
I think your problem is here:

Serial.println(addValue(500, brain_data));

What are you trying to do there? Inside the print function you are adding a value to the array, moving all the array values around and then returning the whole array (by the way, what would your line "return array" return?).
I think if you want to print all the values in the array, you would be better of doing something like:

for (int i=0; i<=30; i++) {
    Serial.println(brain_data[i]);
}

You could do this either before or after adding the new value, depending on what array values you want to print.

As to your addValue function, i think i would do it starting from the "end" of the array, something like

int addValue(int number){
  for (int i=29; i>=1; i--){
    brain_data[i] = brain_data[i-1];
  }
  brain_data[0] = number;
}

because you have defined your array on the beginning (global variable), i think you actually don't need to return it. This function will update its values, so when you make the serial.print() (like my above example) you should always be printing the latest values.

well, i didn't test this code, but it was the idea i wanted to demonstrate

As @boguz has said, your array is a global variable so it is available to all the code in your sketch. There is no need to pass it as a paramter or to use pointers.

You can't easily make an array longer (add extra elements) in the limited memory of the Arduino so start with an array that is big enough for whatever you will eventually need.

Whether programming on the Arduino or on a PC don't think of moving the elements in the array. Just change the array index so it appears as though the contents have been moved. If you have an array with 5 items [0-4] and you no longer want the data in [ 0] just replace it with the new data. Then if you want the [ 1] data to be the head of the array just treat that as the start so the array is organized [ 1-4, 0].

...R

My [url=https://github.com/majenkotech/Average]Average library does this kind of operation. Here's the "push" function I use:

template <class T> void Average<T>::push(T entry) {
    for(uint32_t i = _size - 1; i > 0; i--) {
        _store[i] = _store[i-1];
    }
    _store[0] = entry;

    if (_count < _size) {
        _count++;
    }
}

_store is the array, which you could pass to your function. _count is the current number of valid entries in the array, and _size is the total size of the array. If you're starting with a filled array then _size and _count would always be the same and you can simplify it accordingly.

int brain_data[30]={82960,

Nonsense right there. Use the correct type for the array.

PaulS:

int brain_data[30]={82960,

Nonsense right there. Use the correct type for the array.

Might be using a Due... that's 32-bit, and so "int" would be fine...

Hello all,
Thanks for the responses.
So I redid the function so it uses the global variable of the array.
The problem is when I print out the new array, all the values are 17424 except the first one which is 500

long brain_data[30]={82960, 21827, 39586, 34202, 47124, 69690, 9468, 47798, 6563, 47456, 42494, 66574, 92910, 19151, 23587, 155965, 45245, 33233, 23437, 22788, 168338, 4015, 20487, 44382, 8065, 85082, 5408, 147608, 109213, 109210};

void  addValue(int number){
   for (int i=0; i<29; i++){
    int array_val;
    array_val=brain_data[i];
    brain_data[i+1]=array_val; 
   }
   brain_data[0]=number;
  
  
}
void setup()
{
  Serial.begin(9600);
 addValue(500);
 for (int i=0; i<30; i++)
 {
  Serial.println(brain_data[i]); 
 }
}
void loop(){
  
}

that is because you are replacing the values starting from the beginning of the array.
Just think about it, if you say that:

array[i+1] = array[1];

when i = 0, the second element of the array will assume the same value as the first.
Next time, i = 1, the third element will assume the value of the second (which in turn is already equal to the first).
and so on until the end...
If it wasn't for that last statment:
brain_data[0]=number; you would actually end up with all the values in the array having the same value.

I think the easiest way to do it would be like i suggested before: start from the end of the array. And other people have suggested other ideas that would also work.

You also have int array_val; when it should be long array_val;

...R