user27311: I have a value for led brightness that is stored as an unsigned char (0-255)
unsigned char* colors[3];
colors[0] = 255;
colors[1] = 0;
colors[2] = 0;

I want to multiply a brightness percentage to that value.
float percent = 0.5;
auto red_brightness = colors[0] * percent

Since colors[0] is of type char and percent is of type float. It gives error Invalid operands of types 'unsigned char*' and 'float' to binary 'operator*'
unsigned char brightness;
float percent;
unsigned char* colors[3];

void setup() {
  Serial.begin(9600);
  colors[0] = 255;
  colors[1] = 0;
  colors[2] = 0;
}

void loop() {
    brightness = 127;
    float percent = (float)brightness / 255;
    float newRed = (float)colors[0] * percent;
    
    Serial.println(percent);
    Serial.print("red ");
    Serial.print(newRed);
    Serial.println("");
    delay(1000);
}

How can I multiply colors[0] (255) by 0.5 to get 127?

What I've tried

Cast colors[0] to a float or int

    # invalid cast from type 'unsigned char' to type 'float'

    float newRed = (float)colors[0] * percent;


strtod

Using this conversion website, I've tried converting like so:
    float tempColor = (float)strtod(colors[0],NULL);
    float newRed = tempColor * percent;

While this compiles, it always returns 0 (not 127 like I expect)
unsigned char brightness;
float percent;
unsigned char* colors[3];

void setup() {
  Serial.begin(9600);
  colors[0] = 255;
}

void loop() {
    brightness = 127;
    float percent = (float)brightness / 255;
    float tempColor = (float)strtod(colors[0],NULL); // <- returns 0.00

    Serial.print("tempColor");
    Serial.println(tempColor);
    delay(1000);
}

How can I multiply 255 by 0.5 to get ~127 ?

Solution
Thanks to the pointer by st2000 and this sparkfun article on datatypes
I avoided floating point and char arrays and converted everything to byte and now my code works as expected. Thank you
byte brightness;
byte percent;
byte colors[3];

void setup() {
  Serial.begin(9600);
  colors[0] = 255;
}
void loop() {
      percent = 50;
      auto foobar = colors[0] * percent / 100;
      Serial.print("foobar: ");
      Serial.println(foobar);

11:12:19.833 -> foobar: 127


user20245: Using "floats" is expensive both in required space and execution time.  Consider avoiding the problem entirely and use uint8_t or byte for everything.
Then just assume
percent = 50; 

... is 50%.  Then try:
tempColor =  colors[0] * percent / 100;

... where all variables are of type uint8_t or byte.

user7508: You wrote:

I have an led brightness that is stored as an unsigned char (0-255)
unsigned char* colors[3];


This is not an array of unsigned char, this is an array of
pointers to unsigned char. If you instead write
unsigned char colors[3];

Everything should work as expected.
But then, st2000 has very good advice on how to avoid expensive floating
point operations.

27311: <p>I have a value for led brightness that is stored as an <code>unsigned char</code> (0-255)</p>
<pre><code>unsigned char* colors[3];
colors[0] = 255;
colors[1] = 0;
colors[2] = 0;
</code></pre>
<p>I want to multiply a brightness percentage to that value.</p>
<pre><code>float percent = 0.5;
auto red_brightness = colors[0] * percent
</code></pre>
<p>Since <code>colors[0]</code> is of type <code>char</code> and <code>percent</code> is of type <code>float</code>. It gives error <code>Invalid operands of types 'unsigned char*' and 'float' to binary 'operator*'</code></p>
<pre class="lang-cpp prettyprint-override"><code>unsigned char brightness;
float percent;
unsigned char* colors[3];

void setup() {
  Serial.begin(9600);
  colors[0] = 255;
  colors[1] = 0;
  colors[2] = 0;
}

void loop() {
    brightness = 127;
    float percent = (float)brightness / 255;
    float newRed = (float)colors[0] * percent;
    
    Serial.println(percent);
    Serial.print(&quot;red &quot;);
    Serial.print(newRed);
    Serial.println(&quot;&quot;);
    delay(1000);
}
</code></pre>
<p>How can I multiply <code>colors[0]</code> (255) by <code>0.5</code> to get <code>127</code>?</p>
<hr />
<h2>What I've tried</h2>
<ol>
<li>Cast <code>colors[0]</code> to a float or int</li>
</ol>
<pre><code>    # invalid cast from type 'unsigned char' to type 'float'

    float newRed = (float)colors[0] * percent;
</code></pre>
<ol start="2">
<li>strtod</li>
</ol>
<p>Using <a href="https://www.convertdatatypes.com/Convert-char-Array-to-float-in-CPlusPlus.html" rel="nofollow noreferrer">this conversion website</a>, I've tried converting like so:</p>
<pre><code>    float tempColor = (float)strtod(colors[0],NULL);
    float newRed = tempColor * percent;
</code></pre>
<p>While this compiles, it always returns <code>0</code> (not <code>127</code> like I expect)</p>
<pre class="lang-cpp prettyprint-override"><code>unsigned char brightness;
float percent;
unsigned char* colors[3];

void setup() {
  Serial.begin(9600);
  colors[0] = 255;
}

void loop() {
    brightness = 127;
    float percent = (float)brightness / 255;
    float tempColor = (float)strtod(colors[0],NULL); // &lt;- returns 0.00

    Serial.print(&quot;tempColor&quot;);
    Serial.println(tempColor);
    delay(1000);
}
</code></pre>
<p>How can I multiply <code>255</code> by <code>0.5</code> to get <code>~127</code> ?</p>
<hr />
<h2>Solution</h2>
<p>Thanks to the pointer by st2000 and <a href="https://learn.sparkfun.com/tutorials/data-types-in-arduino/defining-data-types" rel="nofollow noreferrer">this sparkfun article on datatypes</a></p>
<p>I avoided floating point and char arrays and converted everything to <code>byte</code> and now my code works as expected. Thank you</p>
<pre><code>byte brightness;
byte percent;
byte colors[3];

void setup() {
  Serial.begin(9600);
  colors[0] = 255;
}
void loop() {
      percent = 50;
      auto foobar = colors[0] * percent / 100;
      Serial.print(&quot;foobar: &quot;);
      Serial.println(foobar);
</code></pre>
<pre><code>11:12:19.833 -&gt; foobar: 127
</code></pre>
 20245: <p>Using &quot;floats&quot; is expensive both in required space and execution time.  Consider avoiding the problem entirely and use <a href="https://stackoverflow.com/questions/10457210/arduino-uint8-t-variables">uint8_t or byte</a> for everything.</p>
<p>Then just assume</p>
<pre><code>percent = 50; 
</code></pre>
<p>... is 50%.  Then try:</p>
<pre><code>tempColor =  colors[0] * percent / 100;
</code></pre>
<p>... where all variables are of type uint8_t or byte.</p>
 7508: <p>You wrote:</p>
<blockquote>
<p>I have an led brightness that is stored as an <code>unsigned char</code> (0-255)</p>
<pre class="lang-cpp prettyprint-override"><code>unsigned char* colors[3];
</code></pre>
</blockquote>
<p>This is not an array of <code>unsigned char</code>, this is an array of
<strong>pointers</strong> to <code>unsigned char</code>. If you instead write</p>
<pre class="lang-cpp prettyprint-override"><code>unsigned char colors[3];
</code></pre>
<p>Everything should work as expected.</p>
<p>But then, st2000 has very good advice on how to avoid expensive floating
point operations.</p>
