C++ 指向指针的指针

标签 c++ pointers constants

我想要一个指向常量的指针,并将其地址传递给一个函数,该函数会递增它。

int f1(const int **ptr) {
    int n = **ptr; //use pointer
    (*ptr)++; //increment pointer
    return n;
}

void foo(const int *data) {
    const int *p = data;
    const int n = f1(&p); //error: invalid conversion from ‘const int**’ to ‘int**’
                          //error:   initializing argument 1 of ‘int LevelLoader::readWord(byte**)’


}

如何声明指针?

最佳答案

尝试

int f1(const int *& ptr) {
  int n = *ptr;
  ++ptr; 
  return n;
}

void foo(const int *data) {
  const int *p = data;
  const int n = f1(p); 
}

相反。

错误消息表明 LevelLoader::readWord 没有采用 const byte**

关于C++ 指向指针的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729998/

相关文章:

c++ - 为什么将语言标准从 -std=gnu++98 提升到 -std=gnu++11

c++ - 减少 operator= 和复制构造函数之间的代码重复

c++ - 赋值运算符应该为数据成员分配新内存还是重用现有内存?

c# - static 和 const 变量之间的区别

c++ - 任意类的 const 和非 const 成员函数的模板包装器

c++ - boost.pool 是如何实现分配内存的重用呢?

c++ - 使用 C++/CUDA 和 CImg 将交错数据的图像数组转换为非交错数据的问题

c# - 将字符串分配给指向字符的指针

c 使用 freopen 和 argv[1] 在终端中创建文件

javascript - 使用 Angular JS 将常量注入(inject)其他模块配置