Modifying an array in void function

Probably a stupid question, but is there a way to modify an array in a void function to be used outside of that function?

consider the structure

byte array[6];
byte *ptr=&array[0];

void function()
{
while (blabla)
{
*(ptr+i)=something;
i++;
}
}

void loop()
{
void function()
//uses array
}

With that code, my array is always empty. I have to do this instead

byte function()
{
while (blabla)
{
*(ptr+i)=something;
i++;
}
return 0;
}

void loop()
{
byte uselessvariable=function()
//uses array
}

The code works, but I'd like to get rid of that useless variable. Is that possible with a void type?

Why don't you post your actual code?

The return type of a function has no bearing on the function's ability to modify a global array.

my actual code is kinda long, messy and commented in french, but here it is anyway.

#include <Wire.h>
#include <string.h>
#include <avr/interrupt.h> 
#include <avr/io.h>
#undef int
#include <stdio.h>
#include <Servo.h> 

Servo patind;
Servo pating;
byte outbuf[6];
byte *bufpoint=&outbuf[0];// stockage des données du Nunchuck
byte zero=0x00;
int deg=0;
int degf=0;
int count=0;
boolean cd=1;  // vérifie le contact des patins avec le sol
boolean cg=1;  // cd:droit cg:gauche
long mil[2];
byte pente=2;

void setup ()
{
  mil[1] = 0;
  pinMode(52, OUTPUT);
  digitalWrite(52, HIGH);
// TCCR1B=13;
// TIFR1=0x00;
// TIMSK1=2;
// OCR1A=174; 
// TCCR3B=13;
// TIFR3=0x00;
// TIMSK3=2;
// OCR3A=1024; 
  patind.attach(50);
  pating.attach(51); // Liaision de la pin 2 avec le contrôleur du moteur 
  Wire.begin ();      // Initialisation du I2C pour le nunchuck
  nunchuck_init ();   // Initialisation du Nunchuck
  Serial.begin (19200);  
  Serial.print ("Finished setup\n");
  TCNT2=0x00; 
   
}

//ISR(TIMER1_COMPA_vect)          // timer compare interrupt service routine
//{
//  while (0b11 & outbuf[5]==0b11)
//  {
//    if (outbuf[1]<100 && pente>0)
//      pente+=1;
//    else if (outbuf[1]>160 && pente<60)
//      pente-=1;
//    
//  }
//}



void loop()
{
   byte caliss=nunchuck();                  // Appel de la fonction pour recevoir la valeur du joystick
  deg= map(outbuf[1], 131, 230, 0, 180);    // Étalonnage des données pour avoir une valeur entre 0 et 180
mil[0]=millis();
  if (deg>degf && mil[0]-mil[1]>=pente)
    {
    degf+=1;
    mil[1]=mil[0];
    
    }
  else if (deg<degf && deg>0)
    degf=deg; 
  else if (deg<0)
  degf=0;
  if (cd==1)
    patind.write(degf);  // Écris la valeur étalonnée dans le PPM de sortie pour le moteur
  if (cg==1)
    pating.write(degf);
    Serial.println(degf); 

//  if (millis()>1000 && millis()<2000)
//      count+=1;
//else
//      Serial.println (count);  
}

code used for acquiring data from a nunchuck

void nunchuck_init ()
{
  
  Wire.beginTransmission (0x52);	// transmet 0x52 au Nunchuck
  Wire.write (0x40);		        // envoie l'adresse de mémoire
  Wire.write (zero);		        // envoie un zéro
  Wire.endTransmission ();	        // arrête la transmission
}

void send_zero ()
{
  Wire.beginTransmission (0x52);	// transmet 0x52 au Nunchuck
  Wire.write (zero);		        // envoie un zéro
  Wire.endTransmission ();	        // arrête la transmission
}

byte nunchuck ()
{
  int cnt = 0;
  Wire.requestFrom (0x52, 6);	        // Demande les 6 bytes de données du Nunchuck
  while (Wire.available ())
    {
      *(bufpoint+cnt) = nunchuk_decode_byte (Wire.read ());	// Reçoit un byte de données du Nunchuck
      cnt++;
    }
//  Serial.println(outbuf[1]); 
  send_zero ();
return 0;  // Envoie la demande pour recevoir les prochains bits
//  byte joy=outbuf[1];  // Stock la variable du Joystick y dans joy et retourne la valeur
////  Serial.println(joy);
//  return joy;
}

char nunchuk_decode_byte (char x)       // Décodage des valeurs du Nunchuck
{
  x = (x ^ 0x17) + 0x17;
  return x;
}

The function called is nunchuck()

void loop()
{
void function()
//uses array
}

That comment is WRONG. That is not how to call a function.

PaulS:

void loop()

{
void function()
//uses array
}



That comment is WRONG. That is not how to call a function.

I meant that I use the array in loop

It would help if you could post the code that didn't work, but extrapolating from what you have above, I assume it's this:

void nunchuck();                  // Appel de la fonction pour recevoir la valeur du joystick

What PaulS is pointing out is that that is a function prototype and has no effect, when what you really want is:

nunchuck();                  // Appel de la fonction pour recevoir la valeur du joystick

I meant that I use the array in loop

If you don't actually call the function that modifies the array, how can you expect the array to be modified?

(Did the OP modify the code after posting ?)

You should always use { } even when you only have one statement after "if" or "else", and learn to properly indent your code. Hit CTRL-T in the Arduino IDE. Subtle bugs might lie in "misindented" code.

(Did the OP modify the code after posting ?)

Yes. I hate when people do that.

PaulS:

(Did the OP modify the code after posting ?)

Yes. I hate when people do that.

Definitely. At least one should append an edit: did this and that.

Thanks, but I feel my question was not clear enough: If I call a void function that change an array declared elsewhere with a pointer, the array resets when the function ends. If I call that same function, but declare it an int or byte or whatever and I let it return a useless variable, the array is correctly modified. Why does that happens? A void function can't change a value that lasts after it ends?

Please, stop calling it a "void function".
It's just a function that happens to not return anything.

If I call a void function that change an array declared elsewhere with a pointer, the array resets when the function ends.

You can't declare an array with a pointer; you have to declare a pointer and point it to some memory.

You question still isn't clear (to me) - can you post some code that displays the sort of thing you think you're seeing?

AWOL:
Please, stop calling it a "void function".
It's just a function that happens to not return anything.

If I call a void function that change an array declared elsewhere with a pointer, the array resets when the function ends.

You can't declare an array with a pointer; you have to declare a pointer and point it to some memory.

You question still isn't clear (to me) - can you post some code that displays the sort of thing you think you're seeing?

the array is declared elsewhere and I use a pointer to modify it in the function.

OK, so "post some code" means something different to you.
Like this?

char array [100];

void setup ()
{

}

void loop ()
{
  char* p = array;
  p [0] = 'A';
}

// or this

void foo (char* p)
{
  p [1] = 'B';
}

mxmcharbonneau:
Thanks, but I feel my question was not clear enough: If I call a void function that change an array declared elsewhere with a pointer, the array resets when the function ends. If I call that same function, but declare it an int or byte or whatever and I let it return a useless variable, the array is correctly modified. Why does that happens? A void function can't change a value that lasts after it ends?

  • There is no void function, nor int function, nor anything like that. There are functions that return data, and one must declare what type that data is, and there are functions that don't return anything, and one tells that to the compiler by using the special type void.

  • The data type returned by a function, be it void or anything else, has no influence whatsoever on what happens to the variables used inside that function. That depends only on the function code. What you are observing is definitely not a direct effect of what type of data that function returns.

  • "Post the code" in this context means two things:

  1. copy-n-paste into a reply the entire code where you use the "void function"
  2. copy-n-paste into a reply the entire code where you use the "int function"
    Highlight the differences between the two codes and how they differ in their behaviour.

IMHO this is the only way you can get some help.

void loop()
  {
  byte uselessvariable=function();
  //uses array
  }

Can be replaced by:

void loop()
  {
  function();
  //uses array
  }

In other words you can ignore the return code. And then if you are planning to ignore it, declare "function" as (returning) void - that is, nothing.