Pointer Issues in Custom Library

Hey fellow ArduHeads,

So I’ve been working on my first library, that is meant to use the WiFi101 library and enable a user to create a wifi set up involving a web form. I’ve called it APSetupParser since it’s main job is to read back form info. My first issues were bringing in variables that change outside of the library.

APSetupParser.cpp

#include "Arduino.h"
#include "APSetupParser.h"
#include "WiFi101.h"

APSetupParser::APSetupParser(WiFiClient client, int* wifiStatus)
{
  wifistatus = &wifiStatus;
  _client = client;
}

void APSetupParser::connectToWiFi(){
  Serial.begin(115200);
  while(!Serial);
  Serial.println(**wifistatus);
}

APSetupParser.h

/*
  APSetup.h - Library for setting up an Arduino with WiFI.
  Created by Samuel Archibald, Junly 13, 2018.
  Released into the public domain.
*/

#ifndef APSetupParser_h
#define APSetupParser_h
#include "Arduino.h"
#include "WiFi101.h"

class APSetupParser
{
    public:
        APSetupParser(WiFiClient client,  int* wifiStatus);
        void connectToWiFi();
    private:
        WiFiClient _client;
        int** wifistatus;
};
#endif

Test Script

#include <WiFi101.h>

#include <APSetupParser.h>

WiFiClient client;
int var = 0;
int *a = &var;
APSetupParser parser(client, a);

void setup() {

}

void loop() {
  // put your main code here, to run repeatedly:
  parser.connectToWiFi();
  var+=1;
}

What I want to happen is output a counter to the screen, showing that changing the int var outside of the library can be referenced inside the library. I’ve written many Python libraries, but I’m new to C++ libs and this is very confusing. The output I’m getting is a constant -1 (Even if I add a delay to prevent sudden overflow) instead of the value of the integer at vars address.

What is the correct way to pass a pointer of var into a c++ function, and store it, then read it later? I’ve been trying for hours :smiley:

Hey, I m new to this forum too, so don't judge the quality of my answer,

i will bet for defining a public variable in your header (.h) output that from your (.cpp) and make
a simple equality under the condition of your wishin your (.ino) file.

You're de-referencing the pointer one too many times. This is what you want:

class APSetupParser
{
    public:
        APSetupParser(int* wifiStatus);
        void connectToWiFi();
    private:
        int* wifistatus;
};

APSetupParser::APSetupParser(int* wifiStatus)
{
  wifistatus = wifiStatus;
}

void APSetupParser::connectToWiFi(){
  Serial.begin(115200);
  while(!Serial);
  Serial.println(*wifistatus);
}

int var = 0;
int *a = &var;
APSetupParser parser(a);

void setup() {
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  parser.connectToWiFi();
  var+=1;
  delay(1000);
}

Or, you can pass in a reference to the constructor:

class APSetupParser
{
    public:
        APSetupParser(int &wifiStatus);
        void connectToWiFi();
    private:
        int *wifistatusPtr;
};

APSetupParser::APSetupParser(int &wifiStatus)
{
  wifistatusPtr = &wifiStatus;
}

void APSetupParser::connectToWiFi(){
  Serial.begin(115200);
  while(!Serial);
  Serial.println(*wifistatusPtr);
}

int var = 0;
APSetupParser parser(var);

void setup() {
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  parser.connectToWiFi();
  var+=1;
  delay(1000);
}

EDIT:
PS - I left out all the WiFi101 stuff from my version because I don't have that library installed and it's just noise not germane to your problem.

I also combined .h, .cpp, and .ino files because it made it easier to construct a compile-able example and (again) it's not germane to your problem.

APSetupParser.cpp

#include "Arduino.h"
#include "APSetupParser.h"
#include "WiFi101.h"

APSetupParser::APSetupParser(WiFiClient client, int *_wifiStatus)
{
  wifistatus = _wifiStatus;  // Store a local copy of the int *
  _client = client;
}

void APSetupParser::connectToWiFi(){
  Serial.begin(115200);
  while(!Serial);
  Serial.println(*wifistatus);  // Print the int pointed to by the pointer
}

APSetupParser.h

/*
  APSetup.h - Library for setting up an Arduino with WiFI.
  Created by Samuel Archibald, Junly 13, 2018.
  Released into the public domain.
*/

#ifndef APSetupParser_h
#define APSetupParser_h
#include "Arduino.h"
#include "WiFi101.h"

class APSetupParser
{
    public:
        APSetupParser(WiFiClient client,  int *_wifiStatus);
        void connectToWiFi();
    private:
        WiFiClient _client;
        int *wifistatus;
};
#endif

Test Script

#include <WiFi101.h>

#include <APSetupParser.h>

WiFiClient client;
int var = 0;
APSetupParser parser(client, &var);  // Pass the address of var to the int pointer arg

void setup() {
}

void loop() {
  // put your main code here, to run repeatedly:
  parser.connectToWiFi();
  var+=1;
}