The printf format warning is not suitable for a variable of type uint32_t

I just found out about C, and I know this is the main question, but I just can't figure out how I can solve this. For example, I have a line

printf("value :%d\n",var.value); 

The format is not suitable, as it shows an error below

* format '% d expects type' int, but argument 3 is of type 'uint32_t *'

I already checked this link cplusplus: cplusplus print ref

but it does not explicitly indicate how to print a value with type uint32_t * (similar to uint16_t).

Any explanation would be greatly appreciated.

+6
source share
2 answers

You tried to print a pointer to uint32_t as int. You have to do two things:

  • Search for a pointer so that you can type uint32_t, not a pointer.
  • use the correct printtf format specifier.

The correct way to format uint32_t is to use the PRIu32 macro, which expands to a format character as a string.

That is, you do

 printf("%"PRIu32"\n", *var.value); 

You are probably on a common platform where the unsigned int matches uint32_t, in which case you can simply do:

 printf("%u\n", *var.value); 

(note:% u instead of your code that used% d,% u for an unsigned int, and% d for a signed int)

+15
source

If you want the pointer to use %p ,

 printf("value :%p\n",var.value); 

If you want to use the dereferenced value of unsigned int , use

 printf("value :%u\n",*(var.value)); 

It is assumed that the value field in var is actually a pointer to uint32_t - this is what your warning text implies.

It's nice that you get a warning here - printf not type safe, so often an incorrect use of the API simply leads to a sudden malfunction (for example, a crash or worse, undetected memory corruption).

+5
source

Source: https://habr.com/ru/post/891212/


All Articles