gpt4 book ai didi

c++ - 如何在 C/C++ 中描述指向数组的 const 指针?

转载 作者:行者123 更新时间:2023-11-30 18:20:11 25 4
gpt4 key购买 nike

我知道指向 int[10] (ptr->int[10]) 的指针的类型是 int (*var)[10] ,但如何描述那些打击类型呢?

指向const int[10] (ptr->const int[10])的指针的类型

类型为const指向 int[10] 的指针(const ptr->int[10])

类型为const指向 const int[10] (const ptr->const int[10]) 的指针

最佳答案

int (*ptr1)[10] = malloc(sizeof(int)*10);             // Pointer to int[10]
const int (*ptr2)[10] = malloc(sizeof(int)*10); // Pointer to const int[10]
int (* const ptr3)[10] = malloc(sizeof(int)*10); // const Pointer to int[10]
const int (* const ptr4)[10] = malloc(sizeof(int)*10);// const Pointer to const int[10]

*ptr1[0] = 10; // OK.
*ptr2[0] = 10; // Not OK.
*ptr3[0] = 10; // OK.
*ptr4[0] = 10; // Not OK.

ptr1 = realloc(ptr1, sizeof(int)*10); // OK.
ptr2 = realloc(ptr2, sizeof(int)*10); // OK.
ptr3 = realloc(ptr3, sizeof(int)*10); // Not OK.
ptr4 = realloc(ptr4, sizeof(int)*10); // Not OK.

关于c++ - 如何在 C/C++ 中描述指向数组的 const 指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738948/

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