Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
static char* theFruit[] = { "lemon", "orange", "apple", "banana" };
通过查看这个数组,我知道大小是 4。如何以编程方式在 C 中找到这个数组的大小?我不想要以字节为单位的大小。
sizeof(theFruit) / sizeof(theFruit[0])
注意sizeof(theFruit[0]) == sizeof(char *), 一个常数。
sizeof(theFruit[0]) == sizeof(char *)
请改用 const char*,因为它们将存储在只读位置。并获取数组的大小 unsigned size = sizeof(theFruit)/sizeof(*theFruit); *theFruit 和 theFruit[0] 都是相同的。
unsigned size = sizeof(theFruit)/sizeof(*theFruit);