简体   繁体   English

打印 uint64_t 数组的结果

[英]The result of printing an uint64_t array

I have this small piece of code:我有这么一小段代码:

uint64_t test[] = {1, 2, 3, 4, 5};
printf("test value: %llu\n", test);

I try to print the test array, and it gives me this number:我尝试打印test数组,它给了我这个数字:

test value: 140732916721552

Can someone explain this and how an uint64_t array works?有人可以解释一下 uint64_t 数组的工作原理吗? Thank you谢谢

In your code在你的代码中

uint64_t test[] = {1, 2, 3, 4, 5};
printf("test value: %llu\n", test);

%llu tells printf that it shall print a long long unsigned integer. The test part of the printf statement pass a pointer to the first element of the array to printf . %llu告诉printf它应该打印一个long long unsigned printf语句的test部分将指向数组第一个元素的指针传递给printf In other words, there is a mismatch between what you are passing (a pointer) and what you tell printf to print (long long unsigned).换句话说,您传递的内容(指针)与您告诉printf打印的内容(long long unsigned)不匹配。

In C such a mismatch leads to "undefined behavior".在 C 中,这种不匹配会导致“未定义的行为”。 So in general it's not possible to say what will be printed.所以一般来说,不可能说会打印什么。 Any print out will be legal from a C standard point of view.从 C 标准的角度来看,任何打印输出都是合法的。 No print out would also be legal.不打印出来也是合法的。 A program crash would be legal.程序崩溃是合法的。 Anything... would be legal.任何……都是合法的。

It's impossible to say what goes on in general.不可能说一般情况下会发生什么。 On a specific system, it's possible to dig into the low level things and figure out what is going on.在特定系统上,可以深入研究底层事物并弄清楚发生了什么。 On my system the printed value corresponds to the address of the first array element interpreted as a long long unsigned integer. But don't rely on that.在我的系统上,打印值对应于第一个数组元素的地址,该元素被解释为 long long unsigned integer。但不要依赖它。 Other systems may do something completely different.其他系统可能会做一些完全不同的事情。

The code below shows how to correctly print the address of the array and the array elements.下面的代码显示了如何正确打印数组的地址和数组元素。

#include <stdio.h>
#include <inttypes.h>

int main(void) 
{
    uint64_t test[] = {1, 2, 3, 4, 5};
    
    // Print the address where the array is located
    printf("Address of test value is %p\n", (void*)test);
    
    // Print the values of the array elements
    size_t sz = sizeof test / sizeof test[0];
    for (size_t i = 0; i < sz; ++i)
      printf("test[%zu] is %" PRIu64 "\n", i, test[i]);
    
    return 0;
}

Output (note: address may differ in every invocation): Output(注意:每次调用的地址可能不同):

Address of test value is 0x7ffc4ace5730
test[0] is 1
test[1] is 2
test[2] is 3
test[3] is 4
test[4] is 5

When you define an array like this in C, what you are actually doing is storing each of these values sequentially on the stack as separate uint64_t s.当您在 C 中定义这样的数组时,您实际做的是将这些值中的每一个作为单独的uint64_t按顺序存储在堆栈上。 The value assigned to the test identifier is then a pointer to the first of these values, a uint64_t* rather than a uint64_t .分配给test标识符的值是指向这些值中第一个的指针,一个uint64_t*而不是uint64_t When you print test , you are printing the pointer rather than any of the elements, ie the memory address of the first element in your array.当您打印test时,您打印的是指针而不是任何元素,即数组中第一个元素的 memory 地址。

The [] notation is equivalent to []表示法等同于

*(test + i)

ie it dereferences a pointer to the i th element.即它取消引用指向第i个元素的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM