# Casting type void to uint8\_t

**URL:** <https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173>\
**Category:** Programming\
**Created:** [August 7, 2022, 10:09pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173 "2022-08-07T22:09:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paaav](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@paaav](https://forum.arduino.cc/u/paaav)\
**Post date:** [August 7, 2022, 10:09pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/1 "2022-08-07T22:09:41Z")

</div>

Hi, I'm working on esp32-s3 devkitc1 with FreeRTOS. I want to create an instance of a function to be able to call it depending on the given argument:

```cpp
#define RED 2
#define GREEN 44
#define YELLOW 1

typedef int taskProfiler;

taskProfiler REDLEDProfiler = 0;
taskProfiler GREENLEDProfiler = 0;
taskProfiler YELLOWLEDProfiler = 0;

const uint16_t *redLed = (uint16_t*)RED;
const uint16_t *greenLed = (uint16_t*)GREEN;
const uint16_t *yellowLed = (uint16_t*)YELLOW;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(2000);

  xTaskCreate(ledControllerTask, "RED LED Task", 8192, (void*)redLed, 1, NULL);
  xTaskCreate(ledControllerTask, "GREEN LED Task", 8192, (void*)greenLed, 1, NULL);
  xTaskCreate(ledControllerTask, "YELLOW LED Task", 8192, (void*)yellowLed, 1, NULL);

}

void ledControllerTask(void *pvParameters)
{
  pinMode(pvParameters, OUTPUT);
  while(1)
  {
    digitalWrite(pvParameters, digitalRead(pvParameters)^1);
    vTaskDelay(500);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

```

After that I get error:

```cpp
exit status 1
invalid conversion from 'void*' to 'uint8_t' {aka 'unsigned char'} [-fpermissive]

```

How I can properly cast void to uint8\_t in this situation?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 7, 2022, 11:01pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/2 "2022-08-07T23:01:01Z")

</div>

> [@paaav](#):
>
> ```arduino
> const uint16_t *redLed = (uint16_t*)RED;
> const uint16_t *greenLed = (uint16_t*)GREEN;
> const uint16_t *yellowLed = (uint16_t*)YELLOW;
> 
> ```

what do you expect from this when RED GREEN and YELLOW are just constant that will be textually replaced by a value

---

<div class="post-metadata">

**Author:** ![noweare](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noweare/32/987856_2.png) [@noweare](https://forum.arduino.cc/u/noweare)\
**Post date:** [August 7, 2022, 11:40pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/3 "2022-08-07T23:40:06Z")

</div>

use  
int redLed=2

then when you create the function use  
xTaskCreate(ledControllerTask, "RED LED Task", 8192, (void\*) &redLed, 1, NULL);

use that for the other two createtasks. I think that should take care of the errors

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 8, 2022, 6:50am UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/4 "2022-08-08T06:50:45Z")

</div>

There is probably no need to cast the pointer to a void\*.

```cpp
xTaskCreate(ledControllerTask, "RED LED Task", 8192, &redLed, 1, NULL);

```

Just need to ensure redLed has memory allocated

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 8, 2022, 10:08am UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/5 "2022-08-08T10:08:41Z")

</div>

it's common for generic function argument to be defined as void\* because they allow the largest size variable. presumably the function referenced by the generic function pointer knows how to handle the argment

> [@paaav](#):
>
> `const uint16_t *redLed = (uint16_t*)RED;`

looks like these are constants, not ptrs. presumably you defined them as ptrs because they need to be cast as (void\*) arguments, but they don't need to be ptrs to be cast as void \*

when i tried recasting the void\* argument to int, i get a loss of precision error. but it works when casting it to long. once cast as long it can be cast to smaller types

```arduino
#include <stdio.h>

enum { Red, Green, Blue };
const char color1 = Green;

void
func (
    void *arg)
{
    long col = (long) arg;
    char c = col;
    printf ("%s: %ld %d\n", __func__ , col, c);
}

int
main ()
{
    func ((void*) color1);
    func ((void*) Blue);

    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 8, 2022, 2:12pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/6 "2022-08-08T14:12:28Z")

</div>

> [@gcjr](#):
>
> it's common for generic function argument to be defined as void\*

If it’s a pointer - not a value

An int (default for your enum) is 2 bytes on a UNO and so is a pointer so that works and the actual two bytes were passed but if you wanted to pass a real value like a long then if would fail on architectures where pointers are only two bytes. ➜ bad idea and **not commonly** done for value.

It’s really used for pointers like you find in memcpy() or memcmp()

```arduino
void * memcpy ( void * destination, const void * source, size_t num );

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [August 8, 2022, 3:08pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/7 "2022-08-08T15:08:57Z")

</div>

> [@paaav](#):
>
> I'm working on esp32-s3 devkitc1

The closest I could find is "ESP32S3 Dev Module".

This version compiles without error or warning:

```arduino
const byte REDPin = 2;
const byte GREENPin = 44;
const byte YELLOWPin = 1;

uint16_t redLedArg = REDPin;
uint16_t greenLedArg = GREENPin;
uint16_t yellowLedArg = YELLOWPin;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(2000);

  xTaskCreate(ledControllerTask, "RED LED Task", 8192, &redLedArg, 1, NULL);
  xTaskCreate(ledControllerTask, "GREEN LED Task", 8192, &greenLedArg, 1, NULL);
  xTaskCreate(ledControllerTask, "YELLOW LED Task", 8192, &yellowLedArg, 1, NULL);

}

void ledControllerTask(void *pvParameters)
{
  uint16_t pin = *(uint16_t *)pvParameters;
  pinMode(pin, OUTPUT);
  while (1)
  {
    digitalWrite(pin, digitalRead(pin) ^ 1);
    vTaskDelay(500);
  }
}

void loop()
{
  // put your main code here, to run repeatedly:
}
```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 8, 2022, 6:03pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/8 "2022-08-08T18:03:43Z")

</div>

> [@J-M-L](#):
>
> If it’s a pointer - not a value

here's a definition for a generic function ptr that may be given a variety of types of value -- `void (*fPtr)(void*)`

---

<div class="post-metadata">

**Author:** ![paaav](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@paaav](https://forum.arduino.cc/u/paaav)\
**Post date:** [August 8, 2022, 6:42pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/10 "2022-08-08T18:42:59Z")

</div>

Unfortunately that doesn't work for me

---

<div class="post-metadata">

**Author:** ![paaav](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@paaav](https://forum.arduino.cc/u/paaav)\
**Post date:** [August 8, 2022, 6:53pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/11 "2022-08-08T18:53:33Z")

</div>

That works exactly as I want to. Thanks for help. I also make other solution for my problem:

```cpp
typedef int taskProfiler;

uint8_t RED = 2;
uint8_t GREEN = 44;
uint8_t YELLOW = 1;

taskProfiler REDLEDProfiler = 0;
taskProfiler GREENLEDProfiler = 0;
taskProfiler YELLOWLEDProfiler = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(2000);

  xTaskCreate(ledControllerTask, "RED LED Task", 8192, &RED, 1, NULL);
  xTaskCreate(ledControllerTask, "GREEN LED Task", 8192, &GREEN, 1, NULL);
  xTaskCreate(ledControllerTask, "YELLOW LED Task", 8192, &YELLOW, 1, NULL);

}

void ledControllerTask(void *pvParameters)
{
  uint8_t *pin = (uint8_t *)pvParameters;
  pinMode(*pin, OUTPUT);
  while(1)
  {
    digitalWrite(*pin, digitalRead(*pin)^1);
    vTaskDelay(500);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 8, 2022, 7:16pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/12 "2022-08-08T19:16:55Z")

</div>

What I meant is that you pass the data as a pointer to the data. The data itself does not always fit into the 2 or 4 bytes allocated for the pointer

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 8, 2022, 7:35pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/13 "2022-08-08T19:35:35Z")

</div>

i believe i understand what you're trying to say -- you can pass any type of value as long as you pass a ptr to it. And yes, the C++ compiler generated an error when i tried casting a void\* to a char, but the C compiler only generated a warning.

what i've seen is defining a type of largest possible size to accommodate any type of argument, ptr or not. i've posted code showing how a char can be passed

void\* is an idiom, how can you have a ptr to a void. it means a value with unspecified type (not just void). i know you're going to say "but it's still a ptr".

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [August 8, 2022, 8:06pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/14 "2022-08-08T20:06:01Z")

</div>

> [@gcjr](#):
>
> ```arduino
> void *arg = (void*) color1;
> ...
> long col = (long) arg;
> 
> ```

There's no guarantee that this works, even if the integer type is the same size as a pointer:

From [reinterpret\_cast conversion - cppreference.com](https://en.cppreference.com/w/cpp/language/reinterpret_cast),

> 1. A pointer can be converted to any integral type large enough to hold all values of its type (e.g. to [std::uintptr\_t](https://en.cppreference.com/w/cpp/types/integer))
> 
> 2. A value of any integral or enumeration type can be converted to a pointer type. A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value, otherwise the resulting pointer cannot be dereferenced safely ( **the round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations** ) The null pointer constant [NULL](https://en.cppreference.com/w/cpp/types/NULL) or integer zero is not guaranteed to yield the null pointer value of the target type; [`static_cast`](https://en.cppreference.com/w/cpp/language/static_cast) or [implicit conversion](https://en.cppreference.com/w/cpp/language/implicit_cast) should be used for this purpose.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 9, 2022, 6:57am UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/15 "2022-08-09T06:57:05Z")

</div>

> [@gcjr](#):
>
> i know you're going to say "but it's still a ptr".

Indeed - it is…. Really not a good option.  
If you want to accept various types of data and want to deal with them as a byte array for example then use void\* and pass the pointer.

Anything else is **not** common practice nor recommended

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 9, 2022, 1:19pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/16 "2022-08-09T13:19:28Z")

</div>

> [@J-M-L](#):
>
> Anything else is **not** common practice nor recommended

kinda like "Animal Farm" - there's a reason it's just a warning with the C compiler (must be applications out there where the practice is accepted

i had a supervisor question the use of pointers, suggesting it was a bad practice.

what if the value being passed is a constant?

kernighan mentioned how C doesn't have the guard rails that C++ has for much larger applications

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [August 9, 2022, 7:43pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/17 "2022-08-09T19:43:25Z")

</div>

> [@gcjr](#):
>
> there's a reason it's just a warning with the C compiler (must be applications out there where the practice is accepted

I think the more likely explanation is that they at some point they added a diagnostic for it, and to avoid breaking old code bases, they made it a warning instead of an error.

> [@gcjr](#):
>
> And yes, the C++ compiler generated an error when i tried casting a void\* to a char, but the C compiler only generated a warning.

When casting a pointer to an integer, there are only two main scenarios:

1. The pointer is actually a pointer to something. Converting it to an integer is only meaningful if you either A. later convert it back to a pointer, or B. use it as a unique integer for printing the address, using it as an index in some associative data structure, etc.  
In both cases, converting the pointer to an integer type that's too small to represent all pointer values is a bug.
2. The pointer doesn't actually point to anything, but it's the result of an earlier cast from an integer to a pointer (e.g. `reinterpret_cast<void *>(42)`). In this case, casting the pointer back to an integer is meaningless, because you're not guaranteed to get back the original integer.

> [@gcjr](#):
>
> what if the value being passed is a constant?

Then you ─ _very carefully_ ─ use a `const_cast`: casting a pointer-to-const to a pointer-to-nonconst is allowed, as long as you don't use the pointer-to-nonconst to actually write to a constant object.

> [@gcjr](#):
>
> kernighan mentioned how C doesn't have the guard rails that C++ has for much larger applications

In this case, you can probably use C++'s `std::thread`, which makes this a non-issue, because it passes any arguments to the function, in a type-safe way:

```cpp
void ledControllerTask(uint8_t color);

redTask = std::thread {ledControllerTask, RED};

```

If you need a specific configuration (task name, stack size, pinning to a specific core, etc.), you can use the `esp_pthread_set_cfg()` function before creating your threads.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 9, 2022, 8:12pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/18 "2022-08-09T20:12:06Z")

</div>

> [@gcjr](#):
>
> kernighan mentioned how C doesn't have the guard rails that C++ has for much larger applications

We use a C++ compiler here. C let you do lots of things but the number of bytes allocated for a pointer is known and won’t fit larger data types so even in C it’s not a good idea

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 9, 2022, 8:56pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/19 "2022-08-09T20:56:55Z")

</div>

> [@J-M-L](#):
>
> but the number of bytes allocated for a pointer is known and won’t fit larger data types so even in C it’s not a good idea

i don't understand why when copying data, the assumption is to copy the larger into the smaller instead of copying what fits into the smaller (which appear to be what c++ does).

the following works in C++. the long is truncated the size of char

```arduino
void
func (
    void *arg)
{
    long col = (long) arg;
    char c = col;
    printf ("%s: %ld %d\n", __func__ , col, c);
}

```

```arduino
func: 0x120a 10

```

i don't understand why the compiler reports a loss of precision when going from (void\*) to char, but not when going from long to char.

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [August 9, 2022, 9:24pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/20 "2022-08-09T21:24:39Z")

</div>

> [@gcjr](#):
>
> i don't understand why the compiler reports a loss of precision when going from (void\*) to char,

Because converting a pointer to a small integer type is **always** a bug, see my previous two posts.

> [@gcjr](#):
>
> but not when going from long to char.

Because there's nothing wrong with the conversion from long to char on its own (if you ignore the fact that the long originated from a pointer cast).

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [August 9, 2022, 9:36pm UTC](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173/21 "2022-08-09T21:36:46Z")

</div>

> [@gcjr](#):
>
> i don't understand why the compiler reports a loss of precision when going from (void\*) to char

I get error, but if you cast pointer to intptr\_t it works fine

[Next page](https://forum.arduino.cc/t/casting-type-void-to-uint8-t/1020173.md?page=2)
