gpt4 book ai didi

c - C 中静态数组的大小

转载 作者:行者123 更新时间:2023-12-02 06:06:13 26 4
gpt4 key购买 nike

有人能告诉我在 LOOKUP 数组中查找数组时是否有办法保持数组的正确大小?我觉得这是不可能的,因为一旦您将数组视为 int 指针,C 就会丢失信息。

const int NUMBERS1[] = {1, 2, 3, 4, 5 };
const int NUMBERS2[] = {1, 2, 3, 4, 5 };
const int* LOOKUP[] = { NUMBERS1, NUMBERS2 };


int main()
{
int correctSize = sizeof(NUMBERS1); // correctSize == 20
int wrongSize = sizeof(LOOKUP[0]); // wrongSize == 4
}

最佳答案

不,sizeof 是正确的。 LOOKUP[0]int* 类型,sizeof(int*) 在您的系统上是 4。

I have a feeling it is impossible due to C losing the information as soon as you treat the array as an int pointer.

没错。您必须跟踪长度。

关于c - C 中静态数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com