如何初始化全局常量指针?
我有一个变量,它是链表的头。我想将其设置为常量,因为它永远不应该更改,该变量在整个程序中使用,所以我认为应该将其设置为全局常量。问题是我声明它为 const 后无法初始化它。
我该如何解决这个问题?
typedef struct PT {
int x;
int y;
struct PT *next;
} POINT;
//globals
POINT * const mypoint_head;
int main(int argc, char *argv[])
{
int size = 100;
mypoint_head= InitPoint(size); // error C2166: l-value specifies const object
//rest of code
}
POINT* InitPoint(int size)
{
POINT *tmp;
POINT *orig;
int a = 10;
int b = 1000;
orig = (POINT*) malloc (sizeof(POINT) * size);
if(orig == NULL)
return NULL;
tmp = orig;
for (i = 0; i < size; i++)
{
tmp->x = a++;
tmp->y = b++;
if (i == size -1) {
tmp->next = NULL:
}
else {
tmp->next = tmp+1;
}
tmp++;
}
return orig;
}
I have a variable which is the head to a linked list. I want to make it const because it should never be changed, the variable is used throughout the program, so I thought I should make it a global const. The problem is that I couldn't initialize it after I declared it const.
How can I get around this problem?
typedef struct PT {
int x;
int y;
struct PT *next;
} POINT;
//globals
POINT * const mypoint_head;
int main(int argc, char *argv[])
{
int size = 100;
mypoint_head= InitPoint(size); // error C2166: l-value specifies const object
//rest of code
}
POINT* InitPoint(int size)
{
POINT *tmp;
POINT *orig;
int a = 10;
int b = 1000;
orig = (POINT*) malloc (sizeof(POINT) * size);
if(orig == NULL)
return NULL;
tmp = orig;
for (i = 0; i < size; i++)
{
tmp->x = a++;
tmp->y = b++;
if (i == size -1) {
tmp->next = NULL:
}
else {
tmp->next = tmp+1;
}
tmp++;
}
return orig;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你不能——这就是 const 的全部意义。
You can't - that's the whole point of
const
.您是正确的,声明的变量
const
永远不能更改。不幸的是,您的mypoint_head= InitPoint(size);
行被视为尝试更改变量。您必须在声明const
变量时用一个值对其进行初始化。请尝试这样的操作:
现在,您可以使用以下方法初始化列表:
列表头对象是静态声明的,因此它始终存在,您需要适当调整
InitPoint
参数。您还可以对另一个文件中的指针进行extern
引用,而不必使其指向的对象可直接访问(无论其价值如何)。You are correct in that the variable declared
const
can never be changed. Unfortunately, yourmypoint_head= InitPoint(size);
line counts as trying to change the variable. You have to initialize theconst
variable with a value when it is declared.Try something like this instead:
Now, you can initialize the list using:
The head-of-list object was declared statically, so it always exists and you eill need to adjust your
InitPoint
parameters appropriately. You can also have anextern
reference to the pointer in another file without having to make the object it points to directly accessible (for what it's worth).还没有人提出这样的建议:
这在 C++ 中可能不起作用,因为它可能无法真正为 const 对象提供空间。
顺便说一句:这通常不是一个好的做法。然而,在这种情况下,效果很好。
顺便说一句:您需要检查
InitPoint()
的返回结果,并采取相应的行动(可能调用 exit())。Nobody has suggested this yet:
This may not work in C++, where it may not really supply space for const objects.
BTW: This is not generally good practice. In this case, however, it works out well.
BTW: you'll want to check the return from
InitPoint()
, and act accordingly (call exit(), probably).没有全局常量指针作为其他所有内容的接口。
使用函数:-
don't have a global const pointer as your interface to everything else.
use a function :-
从全局声明中删除 const 限定符,并将 rest_of_code 声明为采用指针的 const 限定版本的函数。
Remove the const qualifier from the global declaration and declare rest_of_code as a function that takes the const-qualified version of the pointer.
您必须在声明中使用初始化程序:
然后更改 InitPoint 函数以获取指向数据空间的指针,而不是调用
malloc
并传递它mypoint_data
您也可以坚持
extern POINT * const mypoint_head;
在头文件中,以便可以在其他编译单元中访问。You have to use an initializer in the declaration:
Then change your InitPoint function to take a pointer to the dataspace instead of calling
malloc
and pass itmypoint_data
You can also stick
extern POINT * const mypoint_head;
in a header file so its accessable in other compilation units.(假设 c)const 在声明时必须被初始化并且不能被求值。也许您可以使用 const 指针并使其指向您的头部并将其暴露给代码的其余部分。
如果我必须实现,我可能会通过 getListHead() 函数公开该变量并给它一些晦涩的名称:)
但想知道用例是什么。即使列表的头指针发生变化,我也会让列表正常工作。 (如果我有一个常量头,如果我必须删除头节点,我将不得不像在数组中一样移动元素)
(assumes c) const have to be initialized when they are declared and they cannot be evaluated . Maybe you could use a const pointer and make it point to your head and expose it to the rest of the code.
If i have to achieve may be i will expose the variable by a getListHead() function and give it some obscure name :)
Wonders what the use case is though. I would make my list work even if its head pointer changes. (if i have a const head, if i have to delete the head node i will have to shift the elements like in an array)
我不敢相信还没有人建议使用 const_cast 。它对于常量指针同样有效。
就这么简单,没有额外的变量声明,什么都没有。
I can't believe that no one has yet suggested const_cast.. It works just as fine for constant pointers.
Simple as that, no extra variable declaration, nothing.