如何初始化全局常量指针?

发布于 2024-09-06 18:28:19 字数 847 浏览 3 评论 0原文

我有一个变量,它是链表的头。我想将其设置为常量,因为它永远不应该更改,该变量在整个程序中使用,所以我认为应该将其设置为全局常量。问题是我声明它为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

通知家属抬走 2024-09-13 18:28:19

你不能——这就是 const 的全部意义。

You can't - that's the whole point of const.

一念一轮回 2024-09-13 18:28:19

您是正确的,声明的变量 const 永远不能更改。不幸的是,您的 mypoint_head= InitPoint(size); 行被视为尝试更改变量。您必须在声明 const 变量时用一个值对其进行初始化。

请尝试这样的操作:

//globals
static POINT head_of_list;
POINT* const mypoint_head = &head_of_list;

现在,您可以使用以下方法初始化列表:

mypoint_head->next= InitPoint(size-1);

列表头对象是静态声明的,因此它始终存在,您需要适当调整 InitPoint 参数。您还可以对另一个文件中的指针进行 extern 引用,而不必使其指向的对象可直接访问(无论其价值如何)。

You are correct in that the variable declared const can never be changed. Unfortunately, your mypoint_head= InitPoint(size); line counts as trying to change the variable. You have to initialize the const variable with a value when it is declared.

Try something like this instead:

//globals
static POINT head_of_list;
POINT* const mypoint_head = &head_of_list;

Now, you can initialize the list using:

mypoint_head->next= InitPoint(size-1);

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 an extern reference to the pointer in another file without having to make the object it points to directly accessible (for what it's worth).

小糖芽 2024-09-13 18:28:19

还没有人提出这样的建议:

int main(int argc, char *argv[])
{
    int size = 100;

    // cast address of mypoint_head to a non-const pointer:
    POINT ** nc_pmh = (POINT **)&mypoint_head;
    // use the address to set mypoint_head:
    (*nc_pmh) = InitPoint(size);
    //rest of code
}

这在 C++ 中可能不起作用,因为它可能无法真正为 const 对象提供空间。

顺便说一句:这通常不是一个好的做法。然而,在这种情况下,效果很好。

顺便说一句:您需要检查 InitPoint() 的返回结果,并采取相应的行动(可能调用 exit())。

Nobody has suggested this yet:

int main(int argc, char *argv[])
{
    int size = 100;

    // cast address of mypoint_head to a non-const pointer:
    POINT ** nc_pmh = (POINT **)&mypoint_head;
    // use the address to set mypoint_head:
    (*nc_pmh) = InitPoint(size);
    //rest of code
}

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).

九公里浅绿 2024-09-13 18:28:19

没有全局常量指针作为其他所有内容的接口。

使用函数:-

static POINT * mypoint_head;

POINT* point_head()
{
    return mypoint_head;
}

don't have a global const pointer as your interface to everything else.

use a function :-

static POINT * mypoint_head;

POINT* point_head()
{
    return mypoint_head;
}
还在原地等你 2024-09-13 18:28:19

从全局声明中删除 const 限定符,并将 rest_of_code 声明为采用指针的 const 限定版本的函数。

//globals
POINT * mypoint_head;

void rest_of_code(POINT* const mypoint_head)
{
    mypoint_head = NULL;    // this errors out now
}
int main(int argc, char *argv[])
{
    int size = 100;
    mypoint_head= InitPoint(size);   // no longer errors out
    //rest of code
    rest_of_code(mypoint_head);
}

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.

//globals
POINT * mypoint_head;

void rest_of_code(POINT* const mypoint_head)
{
    mypoint_head = NULL;    // this errors out now
}
int main(int argc, char *argv[])
{
    int size = 100;
    mypoint_head= InitPoint(size);   // no longer errors out
    //rest of code
    rest_of_code(mypoint_head);
}

み零 2024-09-13 18:28:19

您必须在声明中使用初始化程序:

static POINT mypoint_data[100];
POINT * const mypoint_head = &mypoint_head;

然后更改 InitPoint 函数以获取指向数据空间的指针,而不是调用 malloc 并传递它 mypoint_data

您也可以坚持 extern POINT * const mypoint_head; 在头文件中,以便可以在其他编译单元中访问。

You have to use an initializer in the declaration:

static POINT mypoint_data[100];
POINT * const mypoint_head = &mypoint_head;

Then change your InitPoint function to take a pointer to the dataspace instead of calling malloc and pass it mypoint_data

You can also stick extern POINT * const mypoint_head; in a header file so its accessable in other compilation units.

ま柒月 2024-09-13 18:28:19

(假设 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)

输什么也不输骨气 2024-09-13 18:28:19

我不敢相信还没有人建议使用 const_cast 。它对于常量指针同样有效。

const_cast<POINTER *>(mypoint_head) = InitPoint(size);

就这么简单,没有额外的变量声明,什么都没有。

I can't believe that no one has yet suggested const_cast.. It works just as fine for constant pointers.

const_cast<POINTER *>(mypoint_head) = InitPoint(size);

Simple as that, no extra variable declaration, nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文