The followings are my snippet:
typedef struct{
uint8_t dstEndpt;
uint16_t srcEndpt;
uint8_t * quality;
} DataIndT;
DataIndT * dataIndPtr;
printf("dstEndpt: 0x%02x", dataIndPtr->dstEndpt); <-- print out the value of dstEndpt
printf("dstEndpt: 0x%04x", dataIndPtr->srcEndpt); <-- print out the value of srcEndpt
However, how can I print out the value of quality
?
A UINT8 is an 8-bit unsigned integer (range: 0 through 255 decimal).
Since uint8_t and uint16_t use the same format specifier %u , how does printf() "know" how many bytes to process?
To print an unsigned int number, use the %u notation. To print a long value, use the %ld format specifier. You can use the l prefix for x and o, too. So you would use %lx to print a long integer in hexadecimal format and %lo to print in octal format.
sizeof(uint8_t *) is 8 size of pointer. The size of the pointer depends on the target platform and it's usually 8 bytes on x64, 4 bytes on 32 bit machines, 2 bytes on some 16 bit microcontroller, etc.
However, how can I print out the value of quality ?
You do
printf("%p", (void*) dataIndPtr->quality);
This will print address, since value of pointer is address to object to which pointer points.
To print the object where the pointer points, in this case, you can use format specifiers available for C99 (also need to include inttypes.h
). Of course you also need to dereference the pointer:
printf("%" PRIu8 "\n", *(dataIndPtr->quality));
since quality
is pointer to uint8_t
or
printf("%" PRIu16 "\n", *(dataIndPtr->srcEndpt));
for uint16_t
types.
quality
is a pointer, or like an array, if you want to print the value that points to you need to specify it. with the index or dereferencing it:
printf("quality: %d", *(dataIndPtr->quality));
Using the zero index like if it was an array should also print the value:
printf("quality: %d", dataIndPtr->quality[0]);
Or if what you want is print the value of the pointer itself then Michal's answer is what you want.
To print the value of a pointer, use %p
:
printf("dstEndpt: %p", (void*)dataIndPtr->quality);
Like @Giorgi pointed you can use inttypes.h.
Here is a small example:
#include <inttypes.h>
#include<stdio.h>
int main(void){
uint8_t a = 0;
uint16_t b = 0;
uint32_t c = 0;
uint64_t d = 0;
printf("%" PRId8 "\n", a);
printf("%" PRIu16 "\n",b);
printf("%" PRIu32 "\n", c);
printf("%" PRIu64 "\n", d);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With