assign variable to char array

Hi There , i feel i'm still in the newbie stage but know enough to get into trouble. I'm stuck with one section of code in my sketch that i need guidance on please.

I have written a sketch that retrieves a users configuration settings from a webserver and stores them as variables for use in the sketch. It uses IotWebConf.h and TelegramBot.h for this portion of the code. for %99 of the variables it works as intended however i'm struggling with the last one.

Where i'm stuck is i'm trying to add a variable to initilise the BotToken[] array ?

original and working code is :

const char BotToken[] = "This is where my bots API code is put"; // Defines bot token. Enter your Telegram bot token here.

However i want to change it to use a variable as per below :

 #define STRING_LEN 128

 char stringParamValue3[STRING_LEN];

 IotWebConfParameter stringParam3 = IotWebConfParameter("Telegram API Number", "Telegram API",   
 stringParamValue3, STRING_LEN, "password");

const char BotToken[] = stringParamValue3; // Basically just taken away the "" and added a known variable in its place

Doing so i end up with the below error on compile
initializer fails to determine size of 'BotToken'

So not really understanding whats going on i then try this :

#define STRING_LEN 128

 char stringParamValue3[STRING_LEN];

 IotWebConfParameter stringParam3 = IotWebConfParameter("Telegram API Number", "Telegram API", stringParamValue3, STRING_LEN, "password");

const char BotToken[128] {stringParamValue3}; // I added char length in the [] and added {} from what i was reading up on initializing a char array

I then end up with the error of :

invalid conversion from 'char*' to 'char' [-fpermissive]

Again not really understanding what is going on i try :

#define STRING_LEN 128

 char stringParamValue3[STRING_LEN];

 IotWebConfParameter stringParam3 = IotWebConfParameter("Telegram API Number", "Telegram API", stringParamValue3, STRING_LEN, "password");

const char* BotToken[128] {stringParamValue3}; // I added a * to make it const char*

now the errors moved to a different line of code in the sketch of

TelegramBot bot (BotToken, net_ssl);    // Adds library functions.

and the error is

no matching function for call to 'TelegramBot::TelegramBot(const char* [128], BearSSL::WiFiClientSecure&)'

As you can see i haven't grasped the concept of what is going on and feel like i'm just playing a game of trial and error.

Is it possible someone can guide me on whats going on here and how i go about correcting it please ?

Cheers

const char BotToken[] = stringParamValue3;

That code is broken. Try this.

  1. Remove the const modifier from BotToken declaration. Otherwise, any attempt to modify its contents will fail.

  2. Provide a length in the declaration statement - BotToken[nn].*

char BotToken[128];
  1. Don't use '=' , use strcpy() to transfer the variable into BotToken.

Thanks for that , certainly put me on the right track. I don't have it working just yet but i have learn't something new today which is awesome thanks.

Currently i have :

char BotToken[70] ;
char strcpy(char BotToken,char stringParamValue3);

Serial.print(" BotToken in setup code is : ");
Serial.println(BotToken); // This doesn't show API number in the serial monitor
Serial.println(BotToken[0]); // This doesn't show API number either

Serial.print(" The actual var is : ");
Serial.println(stringParamValue3);// this shows me that the API number is in the actual variable

and it compiles ok now but as per the comments , Bottoken doesn't print out the token in the serial monitor. Have i miss read the instructions for strcpy() ?

cheers

char strcpy(char BotToken,char stringParamValue3);

You don't need a function prototype for strcpy, it is provided for you in a header.
Yours is incorrect anyway.

I did a bit more research and now have it working by doing the following

char *my_strcpy(char *BotToken, char *stringParamValue3)
{
  char *start = BotToken;

  while (*stringParamValue3 != '\0')
  {
    *BotToken = *stringParamValue3;
    BotToken++;
    stringParamValue3++;
  }

  *BotToken = '\0'; // add '\0' at the end
  return start;



}

and trigger it by calling

my_strcpy(BotToken, stringParamValue3);

Thanks

Ummm. Why didn’t you just use strcpy since your version is doing the same thing (except for checking for overlapping source and destination)? Or...better yet strncpy because it is safer.