I'm trying to figure out why sending an array of a struct gets updated if I sent it to a function, but doesn't when I send it to a class. I'd like to have the class update it as well. Just shows my lack of expertise on passing objects, etc. The code below demonstrates the problem. What should I do to correct it?
/*This is a test program to experiment with passing arrays to functions and classes.
* I have it working for passing to functions, but the same model doesn't work when
* passing to a class. The code is based on the example code for scanning a WiFiNetwork,
* though for demo's I've commented out all the actual WiFi code so it should run
* on just about any Arduino platform.
*/
#define NarrSize 10
struct SSIDetc{
char N_ssid[30] = " ";
int Enc_Type;
int ChNo;
};
class wifiStart{
private:
SSIDetc arr[NarrSize];
String NeTemp;
int numSsid;
public:
// the constructor
wifiStart(SSIDetc arr[]) {
numSsid = 5;
}
void Update(){
Serial.println("** Update Networks **");
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
NeTemp="This is a test SSID";
NeTemp.concat(thisNet);
Serial.println(NeTemp);
NeTemp.toCharArray(arr[thisNet].N_ssid, NeTemp.length()+1);
arr[thisNet].Enc_Type = 4;
arr[thisNet].ChNo = 11;
}
Serial.println("End of Update");
}
};
SSIDetc NetArray[NarrSize];
wifiStart wifiS(NetArray);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
// scan for existing networks:
Serial.println("Scanning available networks...");
listNetworks(NetArray); //Passing Array to function
Serial.println("Test population of array");
for (int i=0; i<6; i++){
Serial.println(NetArray[i].N_ssid);
}
String NeTemp="----";
for (int i=0; i<6; i++){
NeTemp.concat(i);
NeTemp.toCharArray(NetArray[i].N_ssid, NeTemp.length()+1);
}
wifiS.Update();
Serial.println("Test population by Class");
for (int i=0; i<6; i++){
Serial.println(NetArray[i].N_ssid);
}
Serial.println("======================");
delay(10000);
}
void listNetworks(SSIDetc arr[]) {
// scan for nearby networks
int numSsid = 5;
String NeTemp;
Serial.println("** Scan Networks **");
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
NeTemp="This is a test SSID string";
NeTemp.concat(thisNet);
Serial.println(NeTemp);
NeTemp.toCharArray(arr[thisNet].N_ssid, NeTemp.length()+1);
arr[thisNet].Enc_Type = 4;
arr[thisNet].ChNo = 11;
}
Serial.println();
}
The results are shown below
canning available networks...
** Scan Networks **
This is a test SSID string0
This is a test SSID string1
This is a test SSID string2
This is a test SSID string3
This is a test SSID string4
Test population of array
This is a test SSID string0
This is a test SSID string1
This is a test SSID string2
This is a test SSID string3
This is a test SSID string4
** Update Networks **
This is a test SSID0
This is a test SSID1
This is a test SSID2
This is a test SSID3
This is a test SSID4
End of Update
Test population by Class
----0
----01
----012
----0123
----01234
----012345
======================
/*This is a test program to experiment with passing arrays to functions and classes.
I have it working for passing to functions, but the same model doesn't work when
passing to a class. The code is based on the example code for scanning a WiFiNetwork,
though for demo's I've commented out all the actual WiFi code so it should run
on just about any Arduino platform.
*/
#define NarrSize 10
struct SSIDetc {
char N_ssid[30] = " ";
int Enc_Type;
int ChNo;
};
class wifiStart {
private:
// SSIDetc arr[NarrSize];
SSIDetc* arr;
String NeTemp;
int numSsid;
public:
// the constructor
wifiStart(SSIDetc _arr[]) {
arr = _arr;
numSsid = 5;
}
void Update() {
Serial.println("** Update Networks **");
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
NeTemp = "This is a test SSID";
NeTemp.concat(thisNet);
Serial.println(NeTemp);
NeTemp.toCharArray(arr[thisNet].N_ssid, NeTemp.length() + 1);
arr[thisNet].Enc_Type = 4;
arr[thisNet].ChNo = 11;
}
Serial.println("End of Update");
}
};
SSIDetc NetArray[NarrSize];
wifiStart wifiS(NetArray);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
// scan for existing networks:
Serial.println("Scanning available networks...");
listNetworks(NetArray); //Passing Array to function
Serial.println("Test population of array");
for (int i = 0; i < 6; i++) {
Serial.println(NetArray[i].N_ssid);
}
String NeTemp = "----";
for (int i = 0; i < 6; i++) {
NeTemp.concat(i);
NeTemp.toCharArray(NetArray[i].N_ssid, NeTemp.length() + 1);
}
wifiS.Update();
Serial.println("Test population by Class");
for (int i = 0; i < 6; i++) {
Serial.println(NetArray[i].N_ssid);
}
Serial.println("======================");
delay(10000);
}
void listNetworks(SSIDetc arr[]) {
// scan for nearby networks
int numSsid = 5;
String NeTemp;
Serial.println("** Scan Networks **");
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
NeTemp = "This is a test SSID string";
NeTemp.concat(thisNet);
Serial.println(NeTemp);
NeTemp.toCharArray(arr[thisNet].N_ssid, NeTemp.length() + 1);
arr[thisNet].Enc_Type = 4;
arr[thisNet].ChNo = 11;
}
Serial.println();
}
In the class you have a private member arr. That is different than the parameter arr in wifiStart(SSIDetc arr[]) which you were not doing anything with. I added the "_" to avoid a scoping name conflict.
I'm trying to figure out why sending an array of a struct gets updated if I sent it to a function, but doesn't when I send it to a class.
i don't see where you are passing a "arr" to a class method. Update() has no arguments, but the class has a private member "arr".
when you pass a variable as an argument to a function, you pass it's value and the argument holding the value is local to the function, allocated on the stack. if you change the value of the argument the, it's value is lost when the function returns and is deallocated from the stack.
if you pass a pointer, and that's what "arr []" represents, you are passing the location of the array in memory and any changes to it will be persistent after the function returns
it is often common to pass pointers for return values to a function and the function returns an integer indicating status: OK, ERROR
Thankyou for your help. It's the one line declaration
SSIDetc* arr;
that I needed. I had tried various ways of doing things in the constructor (&arr)[] with and without values between the brackets. But it works thanks to this particular line. I knew I should be passing by reference but couldn't figure it out. So much thanks
dduehren:
Thankyou for your help. It's the one line declaration
SSIDetc* arr;
that I needed. I had tried various ways of doing things in the constructor (&arr)[] with and without values between the brackets. But it works thanks to this particular line. I knew I should be passing by reference but couldn't figure it out. So much thanks