Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print out the value uint8_t *

Tags:

c

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 ?

like image 558
user3815726 Avatar asked Aug 11 '15 08:08

user3815726


People also ask

What does uint8_t * mean?

A UINT8 is an 8-bit unsigned integer (range: 0 through 255 decimal).

What is the format specifier for uint8_t?

Since uint8_t and uint16_t use the same format specifier %u , how does printf() "know" how many bytes to process?

How to print unsigned int in c++?

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.

What is sizeof uint8_t?

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.


4 Answers

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.

like image 174
Giorgi Moniava Avatar answered Oct 24 '22 07:10

Giorgi Moniava


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.

like image 40
wallek876 Avatar answered Oct 24 '22 07:10

wallek876


To print the value of a pointer, use %p:

printf("dstEndpt: %p", (void*)dataIndPtr->quality); 
like image 40
Michał Szydłowski Avatar answered Oct 24 '22 09:10

Michał Szydłowski


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;
}
like image 22
Michi Avatar answered Oct 24 '22 07:10

Michi