C语言 打印出值 uint8_t *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31937276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print out the value uint8_t *
提问by user3815726
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?
但是,如何打印出 的值quality?
采纳答案by wallek876
qualityis 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:
quality是一个指针,或者像一个数组,如果要打印指向的值,需要指定它。使用索引或取消引用它:
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.
或者,如果您想要的是打印指针本身的值,那么 Michal 的答案就是您想要的。
回答by giorgim
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 objectwhere 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:
要打印指针指向的对象,在这种情况下,您可以使用 C99 可用的格式说明符(也需要包含inttypes.h)。当然,您还需要取消引用指针:
printf("%" PRIu8 "\n", *(dataIndPtr->quality));
since qualityis pointer to uint8_tor
因为quality是指向uint8_t或的指针
printf("%" PRIu16 "\n", *(dataIndPtr->srcEndpt));
for uint16_ttypes.
对于uint16_t类型。
回答by Micha? Szyd?owski
To print the value of a pointer, use %p:
要打印指针的值,请使用%p:
printf("dstEndpt: %p", (void*)dataIndPtr->quality);
回答by Michi
Like @Giorgi pointed you can use inttypes.h.
就像@Giorgi 指出的那样,您可以使用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;
}

