Change global string pointer variable from inside a function?

Hello there!

Firstly i would like to apologize beforehand about this question. I imagine that it must be really straight forward, but i'm a beginner and had nowere to go.

I'm declaring the following pointers, outside all my functions (Global Variable):

#include <ESP8266WiFi.h>

String* IP;
String* MAC;

Then, from inside a function i'm trying to change it's values:

void networkInfo() {
    String* IP = WiFi.localIP().toString();
    String* MAC = WiFi.macAddress();
}

When i try to compile and execute, it gives me the following error:

error: cannot convert 'String' to 'String*' in initialization
String* IP = WiFi.localIP().toString();
                                                           ^

error: cannot convert 'String' to 'String*' in initialization
String* MAC = WiFi.macAddress();
                                                         ^

Please, what am i doing wrong?
Thanks...

Lose the asterisks. You don't need pointers for this.

Lose the String inside the functions too, or you will only access local variables.

Yeah but then i will lose the values. I will pass those variables to another function later on...
So the changes needs to persists.

Here, i just tested without the asterisk and it didnt work, when i call it from a new function it prints:

image

As you can see in the image, it does'nt work outside the networkInfo() function...
I need pointers in order to persist my information.

But that way i am not able to pass the information to another function...

No, you don't. Reread my answer #3.

Just use the global name in these places.

String IP;
String MAC;

void networkInfo() {
    IP = WiFi.localIP().toString();
    MAC = WiFi.macAddress();
    specialPrint(IP, MAC);
}

I didnt understand. I need to pass the variables to another function... When i do it like this, using global names, the values get lost when i query the variable from another function.

did the op try?

#include <ESP8266WiFi.h>

String* IP;
String* MAC;

void networkInfo() {
    IP = WiFi.localIP().toString();
    MAC = WiFi.macAddress();
}

?

I will try both solutions @Whandall @Idahowalker and reply.

@Whandall

This way wont work for me because if i create another function called "sendInfo", and calls it, this is what happens:

(As i said previously, i will need to create a function that gets the string definitions in networkInfo() function. "Why?" You may ask. Order and organization - I will use multiple libraries on this project, so if i do everything in the same function it will get weird and confusing.):

void sendInfo() { cout << "Test: " << IP; }

It literally prints "Test: 0"

@Idahowalker

I tested your way and did a new function called printInfo(), this was the output:

void sendInfo() { cout << "Test: " << IP; }

Output when calling function, after networkInfo function:

Test: 0

:smiling_face_with_tear: :smiling_face_with_tear: :smiling_face_with_tear:

Without your complete code, I just don't believe that you did was I said.

1 Like
String *g_IP;
String *g_MAC;

void networkInfo() {
    IP = "String Test";
    MAC = "String Test 2";
    specialPrint(IP, MAC);
}

Output: Compilation error: 'IP' was not declared in this scope

I'm even testing this on a Replit i made, for easier and faster testing.

Replit - Strings in C

Bro? :smiling_face_with_tear:

String *g_IP;
String *g_MAC;

void networkInfo() {
    g_IP = WiFi.localIP().toString();
    g_MAC = WiFi.macAddress();
    specialPrint(g_IP, g_MAC);

}

Output: Compilation error: cannot convert 'String' to 'String*' in assignment

Does the toString method return a pointer to a String? And take no arguments? Where is it declared or documented?

error: cannot convert 'String' to 'String*' in initialization
String* IP = WiFi.localIP().toString();

seems like a clue. That's from #1.

a7

OK, I'm on fumes here, but a bit of googling found

This website

where these lines of code

  String IPaddress;


///


  IPaddress =  WiFi.localIP().toString();

look like No, toString returns a string, and Yes, no arguments to the toString method.

Back to you.

a7

Hey. I will show you the entire code, at the moment:

// Global Variables
String g_IP;
String g_MAC;

// Prototypes
void networkInfo();
void sendInfo();
void initSerial();

void setup() 
{
  // Initialize
  initSerial();
  networkInfo();
  sendInfo();

}

void loop() 
{   
  sendInfo();
}

void initSerial() 
{
    Serial.begin(115200); // Baudrate NodeMCU
}

void networkInfo() {
  String g_IP = "IP Test: 192.168.1.1";
  String g_MAC = "MAC Test: XX:XX:XX:XX:XX:XX";
}

void sendInfo() {
    delay(500);
    Serial.println(g_IP);
}

So, at the moment the output is LITERALLY nothing. Only a blank space.
networkInfo is not persisting the values im giving the global variables...

@horselessname Sry, your post came in whilstI was typoing this, I am off to the beach and will take a look at what you are saying at another time.

Meanwhile:

Here's what I could make of your Replit.

I don't use Strings or C++ string objects, but I do know about global variables and pointers to them.

This demo is your Replit repaired or adjusted so it make any sense. To me.

Use the wokwi simulator for anything you want to be convincing as an argument here.

// Programa: String de funcao pra outra

// this actually runs in the Arduino environment

String *g_IP;
String *g_MAC;

String IP;
String MAC;

// Prototypes - Coloque as funcoes q vai usar aqui

void networkInfo();
void sendInfo();

// Functions.
void networkInfo() {
  g_IP = &IP;
  g_MAC = &MAC;

  *g_IP = "String Teste";
}


void sendInfo() {
  Serial.print("Test ");
  Serial.println((unsigned long) g_IP);

  Serial.print("And >");
  Serial.print(*g_IP);
  Serial.println("<");
}

// Funcoes SETUP e LOOP
void setup() {
  Serial.begin(9600);
  Serial.println("...\n");

  // Inicialize as funcoes aqui
  networkInfo();
  sendInfo();
}

void loop() {
  // Funcoes em loop
}

/* what?
int main() {
  networkInfo();
  sendInfo();
}
*/

Global variables. Arduino Strings. Don't see your need for pointers at all. I may not understand.

HTH

a7

OK, I took a look.

Do you understnd the difference between a global variable and a local variable?

I'm not in the mood to see who told you to lose the "String" in you function, or how long ago. That makes them local variables.

Play with


void networkInfo() {
  g_IP = "IP Test: 192.168.1.1";
  g_MAC = "MAC Test: XX:XX:XX:XX:XX:XX";
}

And I'll see you on the flip side.

a7

Global Variables can be called from anywhere, Local Variables are only called from withing functions (They are created not outside of functions.)

I was able to solve this tho... I didnt really understand the logic, but here it is (Im on this for many hours):

Declared MAC and IP Address:
Yes, with * in it (So i believe that it became a pointer ?)

String *g_IP;
String *g_MAC;

Initialized Serial (I believe this is why it was showing blank stuff on Serial Monitor)

void initSerial(); // Se n iniciar a Serial n consegue usar o Serial Monitor
void initWifi();

void setup() 
{
  // Functions that will be executed
  initSerial();
  initWiFi();
}

void loop() 
{   
    // Empty. No functions here.
 }

void initSerial() 
{
    Serial.begin(115200); // Baudrate do NodeMCU
}

PS: I removed some of the code on initWifi Func, since it isnt relevant. It just connects to WiFi..
I left the part that i was able to get the IP and MAC address (It worked perfectly)

void initWifi() 
{
  // Obs: Without new String it gives Error.
  // ets Jan  8 2013,rst cause:4, boot mode:(1,6)
  // wdt reset

  // Why? IDK

  g_IP = new String; // Is this needed??
  g_MAC = new String; // Removing new String returns error above
  *g_IP = WiFi.localIP().toString();
  *g_MAC = WiFi.macAddress();

  Serial.print("\nIP Obtained: ");
  Serial.println(*g_IP);  
  Serial.print("MAC Address of Device: ");
  Serial.println(*g_MAC);  
}

Output (A little different because i translated when posting and removed unnecessary lines):

image

I will keep studying pointers and what u guys said here on this post since im still a little confused, but thanks for all your help!

@alto777 Thanks again for your patience