相关疑难解决方法(0)

const int*,const int*const和int const*之间有什么区别?

我总是搞砸了怎么用const int*,const int * constint const *正确的.是否有一套规则来定义您能做什么和不能做什么?

我想知道在任务,传递到职能等方面所做的所有事情和所有不应做的事情.

c c++ int pointers const

1262
推荐指数
15
解决办法
47万
查看次数


常量指针有什么意义?

我不是在讨论指向const值的指针,而是指向const指针本身.

我正在学习C和C++,超越了基本的东西,直到今天我才意识到指针是通过值传递给函数的,这是有道理的.这意味着在函数内部,我可以使复制的指针指向其他值,而不会影响来自调用者的原始指针.

那么有一个函数头是什么意思:

void foo(int* const ptr);
Run Code Online (Sandbox Code Playgroud)

在这样的函数里面你不能让ptr指向别的东西因为它是const并且你不希望它被修改,但是这样的函数:

void foo(int* ptr);
Run Code Online (Sandbox Code Playgroud)

工作也一样好!因为无论如何都会复制指针,即使您修改了副本,调用者中的指针也不会受到影响.那么const的优势是什么?

c c++ pointers const

145
推荐指数
11
解决办法
3万
查看次数

C++ const问题

如果我这样做:

// In header 
class Foo {
void foo(bar*);
};

// In cpp
void Foo::foo(bar* const pBar) {
//Stuff
}
Run Code Online (Sandbox Code Playgroud)

编译器不会抱怨Foo :: foo的签名不匹配.但是如果我有:

void foo(const bar*); //In header
void Foo::foo(bar*) {} //In cpp
Run Code Online (Sandbox Code Playgroud)

代码将无法编译.

到底是怎么回事?我正在使用gcc 4.1.x.

c++ gcc const

8
推荐指数
3
解决办法
1945
查看次数

声明指向const的指针或指向const的const指针作为形式参数

我最近对代码做了一些调整,其中我不得不在函数中更改形式参数.最初,参数类似于以下(注意,结构是之前的typedef):

static MySpecialStructure my_special_structure;
static unsigned char char_being_passed;              // Passed to function elsewhere.
static MySpecialStructure * p_my_special_structure;  // Passed to function elsewhere.

int myFunction (MySpecialStructure * p_structure, unsigned char useful_char)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

之所以进行更改,是因为我可以在编译时定义并初始化my_special_structure,而myFunction从未更改过它的值.这导致了以下变化:

static const MySpecialStructure my_special_structure;
static unsigned char char_being_passed;              // Passed to function elsewhere.
static MySpecialStructure * p_my_special_structure;  // Passed to function elsewhere.

int myFunction (const MySpecialStructure * p_structure, unsigned char useful_char)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我还注意到,当我在程序上运行Lint时,有几个Info 818引用了许多不同的功能.该信息表明"指针参数'x'(第253行)可以声明为指向const".

现在,关于上述问题,我有两个问题.首先,关于上面的代码,既然指针和MySpecialStructure中的变量都没有在函数内改变,那么将指针声明为常量也是有益的吗?例如 -

int myFunction (const MySpecialStructure * const …
Run Code Online (Sandbox Code Playgroud)

c embedded optimization

6
推荐指数
1
解决办法
1万
查看次数

C++新手.关于常量指针的问题

我正在尝试通过一些网络教程学习C++.我没有可用的编译器,否则我会尝试这个.我不确定const指针是什么意思.这只是意味着它总是指向相同的内存地址吗?你为什么要这样做?以下代码是否合法?

...
int * const aPointer = new int;
... //do something with aPointer
delete aPointer;
... //do something else, including possibly more 'new' statements
aPointer = new int;
...
Run Code Online (Sandbox Code Playgroud)

c++ pointers const

5
推荐指数
3
解决办法
1030
查看次数

C中"int const*"的确切含义是什么?

可能重复:
const int = int const?
C中的Const

我想知道C中"int const*"的确切含义,以及嵌入式编程系统中"const int*"和"int const*"之间的小比较.

__kanu

c

0
推荐指数
1
解决办法
3463
查看次数

标签 统计

c ×5

const ×5

c++ ×4

pointers ×4

embedded ×1

gcc ×1

int ×1

optimization ×1