user41631: I have the following Arduino code for configuring a wifi connection through a simple webpage:
I declare char* ssid;. Later I read a value from a SPIFFS config file where q_ssid is declared as a string and successfully populated from the file:
strncpy(ssid, q_ssid.c_str(), 10);

In the line above, I am limiting it to exactly 10 as that is the length of my wifi AP name that this connects to. If I change the initial char* to char ssid[10];, all works well, but SSIDs can have varying names, and if I do not use 10 chars, then the trailing spaces prevent connection in the statement WiFi.begin(ssid, password);.
On the web client handler that reads in values from the user, I use this:
String q_ssid = server.arg("ssid");
strncpy(ssid, q_ssid.c_str(), 10 );    

How can I dynamically assign the size of char* ssid; so that it can be used with the correct number of characters for the AP name that the user has stored from the webpage?
Any help is greatly appreciated.
Thanks.

user31105: What you need to do is creating a fixed string, this can be done in two ways:
static:
char ssid[100];

Where 100 is the maximum length ever possible. Note that this will always use up 100 bytes, even if less are used (e.g. your string length is 5.
dynamic:
char* p_ssid = malloc(100);

This way a pointer is created to a dynamically created string that can hold 100 characters. Disadvantage is that using malloc in a microcontroller can cause empty memory 'holes' when freeing/mallocing a lot.
With either way you can than use a normal strncpy or strcpy (in first case use &ssid to get a pointer to the ssid).
Also don't forget to add 1 to the length because of the trailing \0 to denote the string is ended.
Thus for storing 10 characters, you need an 11 char sized array.

41631: <p>I have the following Arduino code for configuring a wifi connection through a simple webpage:</p>

<p>I declare <code>char* ssid;</code>. Later I read a value from a SPIFFS config file where <code>q_ssid</code> is declared as a string and successfully populated from the file:</p>

<pre><code>strncpy(ssid, q_ssid.c_str(), 10);
</code></pre>

<p>In the line above, I am limiting it to exactly 10 as that is the length of my wifi AP name that this connects to. If I change the initial <code>char*</code> to <code>char ssid[10];</code>, all works well, but SSIDs can have varying names, and if I do not use 10 chars, then the trailing spaces prevent connection in the statement <code>WiFi.begin(ssid, password);</code>.</p>

<p>On the web client handler that reads in values from the user, I use this:</p>

<pre><code>String q_ssid = server.arg("ssid");
strncpy(ssid, q_ssid.c_str(), 10 );    
</code></pre>

<p>How can I dynamically assign the size of <code>char* ssid;</code> so that it can be used with the correct number of characters for the AP name that the user has stored from the webpage?</p>

<p>Any help is greatly appreciated.</p>

<p>Thanks.</p>
 31105: <p>What you need to do is creating a fixed string, this can be done in two ways:</p>

<p>static:</p>

<pre><code>char ssid[100];
</code></pre>

<p>Where 100 is the maximum length ever possible. Note that this will always use up 100 bytes, even if less are used (e.g. your string length is 5.
dynamic:</p>

<pre><code>char* p_ssid = malloc(100);
</code></pre>

<p>This way a pointer is created to a dynamically created string that can hold 100 characters. Disadvantage is that using malloc in a microcontroller can cause empty memory 'holes' when freeing/mallocing a lot.</p>

<p>With either way you can than use a normal strncpy or strcpy (in first case use &amp;ssid to get a pointer to the ssid).
Also don't forget to add 1 to the length because of the trailing \0 to denote the string is ended.
Thus for storing 10 characters, you need an 11 char sized array.</p>
