简体   繁体   English

指向const指针的const指针

[英]const pointer pointing to a const pointer

I'm working with some memory pointers. 我正在使用一些内存指针。 I don't want to use hash defines, please leave that discussion aside. 我不想使用哈希定义,请不要讨论。 I would just like to know why this does not compile: 我只想知道为什么它不能编译:

#include <stdio.h>

static const unsigned long *const pMemAddrA = (unsigned long *) 0x00000200ul;
static const unsigned long *const pMemAddrB = pMemAddrA;

int main (void)
{
    printf("%x", (unsigned int) pMemAddrB);
    return 0;
}

Compiler output gcc: 编译器输出gcc:

||=== TestConst, Debug ===|
 ...main.c|4|error: initializer element is not constant|
||=== Build finished: 1 errors, 0 warnings ===|

EDIT: 编辑:

After reading the answers, I'm happy to know how to go about this problem. 阅读答案后,我很高兴知道如何解决这个问题。

However I do not understand why it is a problem. 但是我不明白为什么这是一个问题。 From what I know static memory gets allocated at program start. 据我了解,静态内存是在程序启动时分配的。 I know there is issue if variables "live" in different files and the order in which the variables are allocated cannot be guaranteed by the compiler. 我知道如果编译器无法保证变量在不同文件中的“活动”状态以及变量的分配顺序,则存在问题。 However, if both variables "live" in the same file - just as both variables living in the same function - I would think the compiler can assure that memory gets allocated in the order of variables being declared in the file, and therefore I don't understand why declaring and initializing a const pointer to another const pointer is an issue. 但是,如果两个变量都“存在”于同一个文件中(就像两个变量都存在于同一个函数中一样),我认为编译器可以确保按文件中声明的变量顺序分配内存,因此我不这样做。无法理解为什么声明和初始化指向另一个const指针的const指针是一个问题。 I'd be happy if someone could enlighten me. 如果有人能启发我,我会很高兴。

Your pointers have file scope, so the initialisers must be constant expressions. 您的指针具有文件作用域,因此初始化程序必须是常量表达式。 pMemAddrA isn't a constant expression, therefore can't be used to initialise a variable with static storage. pMemAddrA不是常数表达式,因此不能用于通过静态存储初始化变量。

It can be used to initialise a variable in block scope, so if you move your declarations inside main (and make at least the second non-static), it will compile: 它可以用来在块范围内初始化变量,因此,如果将声明移到main内部(并至少使第二个非静态),它将编译:

#include <stdio.h>

int main (void)
{

    const unsigned long *const pMemAddrA = (unsigned long *) 0x00000200ul;
    const unsigned long *const pMemAddrB = pMemAddrA;

    printf("%x", (unsigned int) pMemAddrB);
    return 0;
}

If the two pointers must be declared at file scope, there is no way to prevent either repeating the initialising expression, 如果必须在文件范围内声明两个指针,则无法防止重复任何一个初始化表达式,

static const unsigned long *const pMemAddrA = (unsigned long *) 0x00000200ul;
static const unsigned long *const pMemAddrB = (unsigned long *) 0x00000200ul;

or #define ing it. #define它。

You don't describe what "does not work", but I guess you mean that the line 您没有描述什么“无效”,但我想您是说这句话

static const unsigned long *const pMemAddrB = pMemAddrA;

produces the error 产生错误

error: initializer element is not constant

.

The solution is that indeed this initializer is not considered as constant. 解决的办法是,确实不将该初始化程序视为常量。 Instead, a memory area for pMemAddrA is set aside and the value 0x00000200ul is written in there. 相反,对于一个存储区域pMemAddrA被搁置和值0x00000200ul是写在那里。 From there on, it is a value which sits somewhere in memory, and not a constant expression. 从那里开始,它是一个位于内存中某个位置的值,而不是一个常量表达式。

Depending on what you want to do with that, you could add another pointer indirection such as 根据您要执行的操作,可以添加另一个指针间接寻址,例如

static const unsigned long *const * const pMemAddrB = &pMemAddrA;

and access it with *pMemAddrB instead of pMemAddrB . 并使用*pMemAddrB而不是pMemAddrB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM