c - char str[] 中大括号之间的值是什么意思?

标签 c string

明确地说,我是 不是 询问 char *str 和 char str[] 之间的区别。我比那更像是一个初学者。我正在实现以下代码:

// Convert long long to string 
    char number_str[];
    sprintf(number_str, "%lld", number_ll);

当我跑 printf("%s\n", number_str); ,它会输出正确的数字,无论我在这些括号之间放 2 还是 256。这让我问,这些括号是做什么用的?

我被曝char str[]this post .

最佳答案

When I run printf("%s\n", number_str);, it spits out the correct number, no matter if I put 2 or 256 between those brackets. Which leads me to ask, what are these brackets for?



这是因为您的代码调用了 未定义的行为 .您尚未为阵列分配空间。

What is the meaning of the value placed between the braces in char str[]?



这意味着您要为数组分配多少空间。但请注意,当使用初始化程序时,可以省略此值,即数组的长度(多维数组中的第一维)。

关于c - char str[] 中大括号之间的值是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21211427/

相关文章:

c - 使用 c 和位移来解决特定需求

java - 如何用制表符替换行开头的所有空格?

c - 一个求字符串长度的程序

C 多行宏 : do/while(0) vs scope block

python - 索引错误 : string index out of range - with len()

Java single char 由多个字节表示的字符串

c# - 将不可 Blittable 结构从 C# 编码到 C++

c++ - 如何将 vector<string> 转换为 char* arr[]?

c - 基于堆栈的缓冲区溢出中的段错误

c - 在 C 中打印字符串的随机尾随字符