int * p=null;int * a=*p;
时间: 2023-08-15 15:08:16 浏览: 54
这段代码存在指针错误。首先,`p`被初始化为`null`,也就是空指针,不能直接用来访问内存。其次,在第二行中,`*p`表示访问空指针所指向的内存,会导致程序崩溃。应该将指针`p`指向一个合法的内存地址,例如:
```c
int x = 10;
int *p = &x; // 将指针p指向变量x的地址
int *a = p; // 将指针a指向指针p所指向的内存
```
这样就可以将指针`p`指向一个合法的内存地址,并将其赋值给指针`a`。注意,指针`a`和指针`p`指向的是同一块内存,它们具有相同的值,但是类型为`int *`。
相关问题
int*a=*b=NULL;
This is not a valid C++ statement.
It appears to be trying to declare two pointers, 'a' and 'b', and initialize them to NULL. However, the syntax is incorrect.
To declare a pointer variable in C++, the '*' symbol should come before the variable name, not before the value being assigned. Additionally, 'a' and 'b' should be separate variables, not dereferencing the same pointer.
Here's a corrected example that declares two separate pointer variables and initializes them to NULL:
```
int* a = NULL;
int* b = NULL;
```
void *p = NULL; int a = 3; 怎么将a的值复制给*p
可以通过将指针 `p` 指向变量 `a` 的地址,然后使用解引用操作符 `*` 来将 `a` 的值赋值给 `p`。
示例代码如下:
```
void *p = NULL;
int a = 3;
p = &a; // 将指针p指向变量a的地址
*p = a; // 通过解引用操作符将a的值赋值给p
```
需要注意的是,此时 `p` 的类型为 `void*`,无法直接进行解引用操作,需要先转换为对应类型的指针才能进行操作。