#type casting buf?

17 messages · Page 1 of 1 (latest)

still copper
#

I am just curious as to why do we have to type cast buf to char, and what does this type cast do? Does it change the data type of buf to char?

uint8_t buf[12];
    int16_t val;
    float temp_c;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  uint16_t lux;
  int steps = 100;

  while (1)
  {
      buf[0] = REG_LUX;
      ret = HAL_I2C_Master_Transmit(&hi2c1, BH1750_addr, buf, 1 , HAL_MAX_DELAY);
      if (ret != HAL_OK) {
          strcpy((char*)buf, "ERROR tx \r\n");
      } else {
          ret = HAL_I2C_Master_Receive(&hi2c1, BH1750_addr, buf, 2, HAL_MAX_DELAY);
          if (ret != HAL_OK) {
              strcpy((char*)buf, "ERROR rx \r\n");
          } else {
              // Combine the two bytes to get the lux value
              lux = ((uint16_t)buf[0] << 8) | buf[1];
              sprintf((char*)buf, "Lux: %u\r\n", lux);
          }
      }
formal nacelleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sharp sparrow
#

it changes how the data is interpreted. and it's needed here because the string functions expect char*, not uint8_t*

still copper
#

wdym by it changes how the data is interpreted

#

does it change buf as a whole to char*?

#

so each element is a char*

#

or am i understanding that wrong

sharp sparrow
#

it's changing the pointer that buf decays into, into a char*, so that it can be indexed as an array of char instead of an array of uint8_t

still copper
#

So it’s changing the entire array data type?

keen vapor
# still copper So it’s changing the entire array data type?

so buf is declared like this:

uint8_t buf[10];

it means that buf refers to an object that is a span of 10 elements, each of type uint8_t

it must be noted that as such, it's okay to consider buf as a uint8_t *, as arrays naturally decay to their pointer "equivalent". arrays point to their first element.

doing (char *)buf tells the compiler to cast the array buf (which decay to uint8_t *) as a pointer to a char.

it means that doing * ( (char*)buf ) will give you a char, instead of a uint8_t. in general, it might not give you the first element of the array, but rather the first byte in it (as a char).

in your particular case, there are some expectations that uint8_t is actually implemented as unsigned char, so essentially it's the same (except that char has implementation-defined signedness, so maybe the conversion performs a sign change)


it doesn't modify in any way, the data stored inside the array object. it's how the compiler interprets the pointer (for pointer arithmetic, for example), and what kind of data you'll get after dereferencing

still copper
#

When I do char* buf it type casts the array to store a maximum of 10 pointers to char?

keen vapor
#

no, it type cast the pointer; so the pointer pretends to be a pointer to a char (aka a byte)

#

it doesn't store 10 chars; that's at most a consequence of the fact that (I guess) uint8_t == unsigned char ; but in theory, it doesn't store 10 chars: it's now a pointer to a byte

#

the real object, is still an array of 10 uint8_t