8

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 ?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user3815726
  • 520
  • 2
  • 6
  • 20

4 Answers4

8

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.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
3

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

printf("dstEndpt: %p", (void*)dataIndPtr->quality); 
Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55
  • see this question -http://stackoverflow.com/questions/12633067/p-format-specifier-in-c. – ameyCU Aug 11 '15 at 09:06
  • @ameyCU the value of a pointer is the address of the object it is pointing to. Printing the address of the pointer would involve `&dataIndPtr->quality`, which OP is not interested in – M.M Aug 11 '15 at 09:11
3

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.

wallek876
  • 3,219
  • 2
  • 19
  • 29
  • @this yeah, you're right i didn't notice just copied from the question, i'll update it. – wallek876 Aug 11 '15 at 09:08
  • 2
    @this it is *a* correct specifier. Without repeating our last debate, `%x` can be used for non-negative values of any type narrower than `int` because they undergo the default argument promotions. In practical terms it is a good choice because some libraries don't support the modifiers added in C99 – M.M Aug 11 '15 at 09:09
  • 1
    @MattMcNabb No. %x is for only unsigned int: `o,u,x,X The unsigned int argument...` Did you read the Standard yet? – this Aug 11 '15 at 09:11
  • 1
    @this "The correct one is*: `printf("%d",x);` This is because of default argument promotions as printf() is variadic function. This means that unsigned char value is always promoted to int." http://stackoverflow.com/a/27547422/995714 did you really understand the standard? `uint8_t` will be promoted to `int`, not `unsigned int` – phuclv Aug 11 '15 at 09:31
  • @Lưu Vĩnh Phúc "unsigned char value is always promoted to int." is not always true. On rare platforms, `UCHAR_MAX > INT_MAX`. In those platforms, expect `unsigned char` to be promoted to `unsigned`. (Note the * in your comment's referenced answer.) – chux - Reinstate Monica Aug 11 '15 at 14:11
  • @chux yes I know, I just quote the original answer. In this case `uint8_t` exists, so `CHAR_BIT` must be 8 and `sizeof(char) < sizeof(int)`, therefore `uint8_t` will always be promoted to int – phuclv Aug 11 '15 at 15:57
  • @Lưu Vĩnh Phúc True about `uint8_t` and `CHAR_BIT must be 8`. – chux - Reinstate Monica Aug 11 '15 at 16:37
1

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;
}
Michi
  • 5,175
  • 7
  • 33
  • 58