2

I would like to be able to set the wifi SSID in the setup(), so I can customize it based on the MAC address or something similar. (avoiding to ESP's with the same code having the same SSID). However since it requires a const it does not seem to be possible, or am I missing something?

I'm using platformIO if that matters...

4
  • 1
    const parameter says the function will not change the string. it doesn't require it to be const
    – Juraj
    Sep 22 '20 at 11:02
  • dont't use platformIO if that matters... :) Sep 22 '20 at 11:51
  • @DataFiddler why is that? I love PlatformIO in vscode, it does wonders for me!
    – Ananas_hoi
    Sep 22 '20 at 16:14
  • Sorry @Ananas_hoi, just a (funny?) response to I'm using platformIO if that matters... I don't know PlatformIO enough, but I'm pretty sure the if condition will not apply. Sep 22 '20 at 17:27
1

A const parameter to a function doesn't define what can be passed. It is, instead, a "promise" by the function that it will not modify the data you pass it.

In C when you pass a "string" (which is actually an array of characters) to a function you don't actually pass the string (as in the content). You really just pass a pointer to the location in memory where the string is stored.

It is perfectly possible for a function to then make modifications to that passed "string", which will be modifying the original data in memory. That is not really desirable in many situations.

So by flagging the parameter as const you tell the compiler "From this moment onwards the data pointed to by this variable must not be changed". All attempts to write to the memory using that variable will be blocked by the compiler.

There's no actual difference between, say, a char * and a const char * other than how the compiler treats attempts to write to them.

When you pass a char * to a function that is expecting a const char * the pointer is "upgraded" to a constant so that no attempts to change the data will be allowed.

The converse is not allowed though - you can't (or shouldn't) try and convert a const char * to a char * as on some systems ("Princeton" architecture systems with a single monolithic memory address space) the pointer could be pointing to data stored in flash, and an attempt to write to that will obviously fail.

The function you are calling to set the SSID is merely promising that it won't attempt to change the data you pass it. It doesn't matter if it's a constant or not - both will be treated as a constant and not be changed.

0

The library WifiManager might work for you, it’s got a captive portal to configure your wifi connection on the fly. It’s got quite some configuration options to shape it to your will if I am not mistaken, I hope it serves you well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.