2

I have been trying for a long time how to use a word declared as const string* word = "xxx" as name of void function declared somewhere below in my code.

Here is example with ssid:

const char* ssid = "name_of_ssid";

void (*name of ssid*) () {

⠀//some code

}

I want to use word declared in const char* in quotation marks as name of function. It can not be written just void ssid(). Yes, I can set a name of void function manually, when const char* ssid = "CoffeeWifi" I can type void CaffeeWifi() but I want to do that by this way because my code is specific for this option and explaining this code here would be very difficult. When I change array of const char* word from "xxx" to "yyy" I want to change the name of void from void xxx() to void yyy() as well.

How to do that? What function should I use?

Thank you!

10
  • 5
    There's no way to do that sort of thing in C++. What you could do is write another program that produces C++ code that you could then compile. None of this has to do with Arduino though.
    – timemage
    Apr 14 at 14:28
  • 2
    What is it you hope to achieve with this that you couldn't do with just passing the SSID as a parameter to the function?
    – Majenko
    Apr 14 at 14:31
  • 2
    You could accomplish that with #define instead of assigning a character string.
    – JRobert
    Apr 14 at 14:38
  • 8
    I have feeling, that this is a x-y problem. You have a problem, that you didn't describe, and you think this is the solution, but most likely it isn't. So please describe better, why you want to do this
    – chrisl
    Apr 14 at 15:13
  • 2
    That's mostly up to O.P. If you're just wondering about my opinion: it's a reasonable answer to reasonable speculation of the problem that might be motivating their question. But their question as asked isn't addressed by it. It's not always easy to tell to what degree you should to take a charitable interpretation of a person's question or to get them to be precise. I tend to push for details and less speculation, but there's a line somewhere. If you're wondering about this sort of stuff, probably the thing to do is to read in the help center and ask on the meta site.
    – timemage
    May 2 at 21:50
1

Such a thing is impossible (unless you keep striving; neither have I tried it yet). Judging from your intention, one thing you can do is to create an array of the names of the functions you'd like to use, another array carrying the pointers to the functions and a programme to call the function according to the input text. I haven't tried it myself, so perhaps I may not sound sensible, but that is the only thing that came to my mind.

An example:

String function_names[3] = {"foo", "bar", "quux"}; /* I am not sure of this syntax; 
                                                corrections are welcome */
int *functions[3] = {&foo, &bar, &quux};

String input;
 
void setup(){
        Serial.begin(9600);
}

void loop(){
        while(Serial.available()){
              String temp_input = Serial.readString()
              input = (temp_input == "")? String("Nothing here") : temp_input;
              execute(input);
        }
        delay(1000);
}

void execute(String func_name){
        for(int i = 0; i < 3; i++){
              if(function_names[i] == func_name){
                     *functions[i]();
              }
         }
}

int foo(){
    Serial.println("foo");
    return(0);
}

int bar(){
    Serial.println("bar");
    return(0);
}

int quux(){
    Serial.println("quux");
    return(0);
}
0

Being a const char, you can set it at compile time with a define and then use it.

#define fstring(x) #x

const char* ssid = fstring(SSID_NAME) ;

void setup() {
  Serial.println("Start");
  Serial.println(ssid);
  SSID_NAME();

}

void loop() {}

void SSID_NAME (){
  Serial.println(ssid);
}

Instead of using quotes, this uses the stringize operator.

3
  • Missing Serial.begin call aside, I don't think this does what you intended it to do. Knowing how the stringize operator works, I'm having a hard time figuring out what the code was suppose to do just by looking at it. You should try it yourself in any case.
    – timemage
    Jun 2 at 13:31
  • Tested on a Leonardo (OP doesn't specify), so doesn't need Serial.begin.
    – user85471
    Jun 2 at 22:44
  • I'm not sure about OP's 'why', but I think this achieves the 'what', but in reverse. Instead of using a string as function name, it makes the function name available as a string.
    – user85471
    Jun 2 at 22:45

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.