# XCreateTask with params (newbie)

**URL:** <https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056>\
**Category:** Kernel\
**Created:** [September 12, 2019, 5:02pm UTC](https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056 "2019-09-12T17:02:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 12, 2019, 5:02pm UTC](https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056/1 "2019-09-12T17:02:32Z")

</div>

**[azz-zza](https://sourceforge.net/u/azz-zza/)** wrote on [Thursday, September 12, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ca9bd87957/#861e):

Hello,  
attempting to pass parameters to xTaskCreate is not working well for me .  
As a reference i use [https://embetronicx.com/tutorials/rtos/freertos/freertos-lpc2148-tutorial-task-creation-with-parameters/](https://embetronicx.com/tutorials/rtos/freertos/freertos-lpc2148-tutorial-task-creation-with-parameters/)

the simplified code is:

```auto
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/gpio.h"

#define BLINK_GPIO GPIO_NUM_2

extern "C"
{
    void app_main(void);
}

void hello_task(void *pvParameter)
{
    char *p;
    p = (char *)pvParameter;
        printf(p);
}

void app_main(void)
{
   xTaskCreate(hello_task, "hello_task", 2048, "1", 5, NULL, NULL);
 
}

```

and it failes with :

```auto
Processing esp32doit-devkit-v1 (platform: espressif32; board: esp32doit-devkit-v1; framework: espidf)
--------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32doit-devkit-v1.html
PLATFORM: Espressif 32 1.9.0 > DOIT ESP32 DEVKIT V1
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: toolchain-xtensa32 2.50200.80 (5.2.0), framework-espidf 3.30202.190627 (3.2.2), tool-esptoolpy 1.20600.0 (2.6.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies

Compiling .pio/build/esp32doit-devkit-v1/src/main.o
src/main.cpp: In function 'void app_main()':
src/main.cpp:86:61: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]
     xTaskCreate(hello_task, "hello_task", 2048, "1", 5, NULL);
                                                             ^
In file included from src/main.cpp:11:0:
/home/az/.platformio/packages/framework-espidf/components/freertos/include/freertos/task.h:432:37: note: initializing argument 4 of 'BaseType_t xTaskCreate(TaskFunction_t, const char*, uint32_t, void*, UBaseType_t, void**)'
  static inline IRAM_ATTR BaseType_t xTaskCreate(
                                     ^
*** [.pio/build/esp32doit-devkit-v1/src/main.o] Error 1

```

Please advise.

---

<div class="post-metadata">

**Author:** ![McuOnEclipse](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mcuoneclipse/32/32_2.png) [@McuOnEclipse](https://forums.freertos.org/u/McuOnEclipse)\
**Post date:** [September 12, 2019, 5:13pm UTC](https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056/2 "2019-09-12T17:13:16Z")

</div>

**[xz8987f](https://sourceforge.net/u/xz8987f/)** wrote on [Thursday, September 12, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ca9bd87957/#b8ef):

Your problem is that you call it with  
xTaskCreate(hello\_task, “hello\_task”, 2048, “1”, 5, NULL, NULL);  
As task additional parameter you pass a pointer to a constant string, but a pointer to void is expected, so use  
(void\*)“1”  
Additionally you pass two NULL parameters after the ‘5’ (which is the task prioritoy). Only one is allowed (for optinal task handle).

Additionally, 2048 would mean 8192 bytes of stack for an ARM Cortex-M (32bit).  
(and cast the result to void if you are not interested in the error code).  
So better write

(void)xTaskCreate(hello\_task, “hello\_task”, 8192/sizeof(StackType\_t), (void\*)“1”, tskIDLE\_PRIORITY+5, NULL);

I hope this helps,  
Erich

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 12, 2019, 5:16pm UTC](https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056/3 "2019-09-12T17:16:43Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, September 12, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ca9bd87957/#1ad3):

You could put an explicit cast to get around the problem, but the issue is that the void pointer passed to the task isn’t made a const pointer because some applications want to pass a writeable buffer through it, which they can’t if the pointer was made const.

In C++, string literals are const, so there pointers will not implicitly decay to a non-const pointer.

That case would work in C, (with perhaps a warning) as in C string literals do not have const type (but shouldn’t be changed) due to historial reasons.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 12, 2019, 5:22pm UTC](https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056/4 "2019-09-12T17:22:08Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, September 12, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ca9bd87957/#861e/b62c):

This looks like a C question, not a FreeRTOS question directly, it just  
happens to be on a FreeRTOS API function.

[https://www.freertos.org/a00125.html](https://www.freertos.org/a00125.html) shows the fourth parameter to  
xTaskCreate() is a void _, and you are passing “1”, and in C strings  
are referenced using a character pointer (char_). In this case the  
string is a const, so you are passing a const char \*. The warning  
generated is therefore not what I would expect, as it says:

invalid conversion from ‘const void\*’ to ‘void\*’

whereas I would expect

invalid conversion from ‘const char\*’ to ‘void\*’

You may be able to avoid the error by casting the const char \* to a void

- as ‘( void \* ) “1”’ - but that might just generate another warning  
about casting away a const - but in any case I doubt that is what you  
intended to do anyway. If you want to pass a pointer to the string “1”  
it would be best to know where the string was allocated - in your code  
it could be allocated anywhere (in RAM, on the stack, in flash as it is  
a const, etc.) and its location is likely to change depending on the  
compiler optimisation level.

Recommend reading the docs available here:  
[https://www.freertos.org/Documentation/RTOS\_book.html](https://www.freertos.org/Documentation/RTOS_book.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 12, 2019, 11:05pm UTC](https://forums.freertos.org/t/xcreatetask-with-params-newbie/8056/5 "2019-09-12T23:05:21Z")

</div>

**[azz-zza](https://sourceforge.net/u/azz-zza/)** wrote on [Thursday, September 12, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ca9bd87957/#f60b):

Gentelmen, enormously helpful. Thank you very much. My C is 3 month old and RTOS is a few days. Thank you for taking time to breakdown the basics for me .  
Cheers.  
Andrew
