简体   繁体   English

重新分配指针给出错误

[英]reassign pointer gives error

I am new to C++ , therefore please forgive me if my question seems basic, I am not able to reassign a pointer when I use the star, here is my code: 我是C ++的新手,因此,如果我的问题似乎很基本,请原谅我,使用星号时我无法重新分配指针,这是我的代码:

int val = 8;
int number = 23;
int *pointer = &val;
*pointer = &number;    //<-- error

this gives an error: assign to int from incompatible type int. 这给出了一个错误:从不兼容类型int分配给int。 here is what I tried: 这是我尝试过的:

pointer = &number;  // this works

therefore my question is, why is this wrong: *pointer = &number 因此,我的问题是,为什么这是错误的:* pointer =&number

In your code, 在您的代码中

 *pointer = &number;    //<-- error

by saying *pointer , you're dereferencing the pointer, which is of type int , then you try to assign a pointer type value to it, which is not correct. 通过说*pointer ,您将取消引用int类型的指针,然后尝试为其分配一个指针类型值,这是不正确的。

What you want to do, is to assign the address of a variable and to hold that, you want the pointer variable itself to be on the LHS of the assignment. 您要做的就是分配变量的地址并保持该地址,您希望指针变量本身位于分配的LHS上。 So, 所以,

 pointer = &number;

is the correct way to go. 是正确的方法。

The reason this results in an error is because you misunderstood the type of each variable. 导致错误的原因是因为您误解了每个变量的类型。

int val = 8; // type of `val` is int
int number = 23; // type of `number` is an int
int *pointer = &val; // type of `pointer` is an int *
*pointer = &number;    // type of `pointer` is int *, so type of `*pointer` is int

It doesn't work because *pointer dereferences the pointer, so you're assigning an address to a pointer, which is not what you want. 这是行不通的,因为*pointer 取消了对*pointer 引用 ,因此您要为指针分配一个地址,这不是您想要的。

When you say 当你说

int *pointer

the * indicates that you are creating a pointer, you can think of the * as part of the data type. *表示您正在创建指针,可以将*视为数据类型的一部分。 ie it's a pointer to an integer type. 即它是一个指向整数类型的指针。 To reference the pointer itself (the type that stores an address in memory), you simply use the name of the pointer, just like a regular variable. 要引用指针本身(在内存中存储地址的类型),您只需使用指针的名称即可,就像常规变量一样。

When you add the * infront, you are dereferencing the pointer. 当添加* infront时,您将取消引用指针。 Meaning, you are accessing whatever is stored at the memory address that the pointer is pointing to, in this case an int. 意味着,您正在访问指针所指向的内存地址中存储的任何内容,在本例中为int。

Basically, pointer gives you an address in memory, *pointer gives you the int stored at that address. 基本上,指针为您提供内存中的地址,*指针为您提供存储在该地址的int。

int *pointer = &val;

Breaking this up actually means 打破这个实际上意味着

int* pointer;
pointer = &val;

When you derefrence an integer pointer it becomes just an integer.When you add & to a non-pointer it becomes a pointer.So doing is bad,because *ptr is not a pointer and &i is a pointer 当您取消对整数指针的引用时,它只是一个整数。当将&添加到非指针中时,它将成为一个指针。这样做很不好,因为* ptr不是指针,而&i是指针

int n = 0;
int *ptr = &n //good,because they are both pointers
int i = 0;
*ptr = &i //bad because only one is a pointer

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

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