const 指针固定为变量

发布于 2024-10-28 23:18:24 字数 985 浏览 4 评论 0原文

我不知道如何告诉 C 我想要一个不会移动的指针。它将始终指向同一个数组。也就是说,数组成员不是恒定的,但数组本身是全局的,因此它位于固定位置。

因此,当我编写此代码时:

#include <stdio.h>

int v[2]={0, 1};
const int *cpv=v;

int main(void)
{
    v[1]=2;     printf("%d\n", v[1]);
    *(cpv+1)=3; printf("%d\n", v[1]);
    cpv[1]=4;   printf("%d\n", v[1]);
}

并出现以下错误:

constp.c: In function ‘main’:
constp.c:9: error: assignment of read-only location '*(cpv + 4u)'
constp.c:10: error: assignment of read-only location '*(cpv + 4u)'

我知道编译器认为我需要一个 const int v[2] 才能与 const int *iv 一起使用。如何获得一个常量指针来完成这项工作?

如果您看到错误消息,则说明我什至没有移动指针(如 pv++)。我只是取消引用它,导致一些字节错位。

如果我这样做:

int *pv=cpv;
*(pv+1)=5;  printf("%d\n", v[1]);
printf("%p == %p !?\n", cpv, pv);

我会收到此警告,但它有效:

constp.c:9: warning: assignment discards qualifiers from pointer target type
pointer# ./constp 
5
0x601020 == 0x601020 !?

谢谢, 贝科。

I can't figure out how to tell C that I want a pointer that will not move. It will always point to the same array. That said, the array members are not constant, but the array itself is global and so, it is at a fixed position.

So, when I code this:

#include <stdio.h>

int v[2]={0, 1};
const int *cpv=v;

int main(void)
{
    v[1]=2;     printf("%d\n", v[1]);
    *(cpv+1)=3; printf("%d\n", v[1]);
    cpv[1]=4;   printf("%d\n", v[1]);
}

And get this errors:

constp.c: In function ‘main’:
constp.c:9: error: assignment of read-only location '*(cpv + 4u)'
constp.c:10: error: assignment of read-only location '*(cpv + 4u)'

I understand that the compiler thinks I need a const int v[2] to use with a const int *iv. How do I get a constant pointer to do the job?

If you see the error message, I'm not even moving the pointer (like pv++). I'm just dereferencing it dislocated some bytes.

If I do this:

int *pv=cpv;
*(pv+1)=5;  printf("%d\n", v[1]);
printf("%p == %p !?\n", cpv, pv);

I get this warning, but it works:

constp.c:9: warning: assignment discards qualifiers from pointer target type
pointer# ./constp 
5
0x601020 == 0x601020 !?

Thanks,
Beco.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

辞旧 2024-11-04 23:18:24

移动 const 限定符:

int *const cpv=v;

说明:在 C 声明规则中,从标识符开始从右向左读取:“cpv 是指向 int 的常量指针”。您的版本将被读取为“cpv是指向int常量的指针”。

请注意,cpv+1 仍会为您提供指向 *cpv 之后的 int 的指针;使指针 const 只能防止 ++--+=-=就可以了。

Move the const qualifier:

int *const cpv=v;

Explanation: in the C declaration rules, this is read from right to left starting at the identifier: "cpv is a constant pointer to int". Your version would be read "cpv is a pointer to int constant".

Note that cpv+1 will still get you a pointer to the int after *cpv; making a pointer const only prevents ++, --, += and -= on it.

南街女流氓 2024-11-04 23:18:24

您使用的是指向 const 的指针,而不是 const 指针。

const int *cpv=v;

应该变成:

int *const cpv=v;

You are using a pointer to const, not a const pointer.

const int *cpv=v;

should turn into:

int *const cpv=v;

一张白纸 2024-11-04 23:18:24

指针是一种“向后读取”的类型:

const int * p;

在本例中,“p”是指向“const int”的(变量)“指针”。从字面上看,向后阅读,“*”表示“指针”:“指向 int const 的指针”。

奇怪的是,下面是同样的事情:

int const * p;  // same thing

向后读,它说,“pointer ... to const int”(同样的事情)。

因此,如果您希望指针是“常量”(而不是变量),则需要向后读取并执行以下操作:

int * const p;  // p cannot change

现在,“p”是指向非常量整数的常量指针。不费吹灰之力,而是向后阅读,“const 指针...指向 int”。

使用上面的“相同的东西”示例,我们现在可以有一个指向 int 的常量指针常量整数(以下内容相同):

const int * const p;
int const * const p;

您应该向后阅读它们,它们都说同样的事情,“常量指针...指向常量 int”。

Pointers kind of "read backwards":

const int * p;

In this case, "p" is a (variable) "pointer" to a "const int". Literally, read that backwards, with the '*' meaning "pointer": "Pointer ... to int const".

Curiously, the following is the same thing:

int const * p;  // same thing

Reading backwards, it says, "pointer ... to const int" (the same thing).

So, if you want your pointer to be "constant" (not variable), you need to read-backwards and do just that:

int * const p;  // p cannot change

Now, "p" is a constant pointer to a non-constant integer. Not to belabor, but reading backwards that's, "const pointer ... to int".

Using the "same thing" example above, we can now have a constant pointer to a constant integer (the following are the same):

const int * const p;
int const * const p;

You should read them backwards, and they both say the same thing, "contant pointer ... to constant int".

铃予 2024-11-04 23:18:24
int * const cpv=v;

话虽如此,为什么还需要指针呢?如果直接访问变量 v,编译器将能够做得更好。如果您通过指针,则每次访问数组时生成的代码都必须从内存中读取指针。

int * const cpv=v;

Having said that, why do you need the pointer at all? The compiler will be able to do a better job if you would access the variable v directly. If you go via a pointer, the generated code have to read the pointer from memory each time you access the array.

萌能量女王 2024-11-04 23:18:24
#include <stdio.h>

int v[2]={0, 1};
//const int       *      cpv=v; // cpv is a pointer to int const
//      int const *      cpv=v; // cpv is a pointer to const int == same as above ==
        int       *const cpv=v; // cpv is a constant pointer to int
//      int const *const cpv=v; // cpv is a constant pointer to const int
//const int       *const cpv=v; // cpv is a constant pointer to int const == same as above ==
//const int const *const cpv=v; // == same as above ==

int main(void)
{
    v[1]=2;     printf("%d\n", v[1]);
    *(cpv+1)=3; printf("%d\n", v[1]);
    cpv[1]=4;   printf("%d\n", v[1]);
}
#include <stdio.h>

int v[2]={0, 1};
//const int       *      cpv=v; // cpv is a pointer to int const
//      int const *      cpv=v; // cpv is a pointer to const int == same as above ==
        int       *const cpv=v; // cpv is a constant pointer to int
//      int const *const cpv=v; // cpv is a constant pointer to const int
//const int       *const cpv=v; // cpv is a constant pointer to int const == same as above ==
//const int const *const cpv=v; // == same as above ==

int main(void)
{
    v[1]=2;     printf("%d\n", v[1]);
    *(cpv+1)=3; printf("%d\n", v[1]);
    cpv[1]=4;   printf("%d\n", v[1]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文