int *a , *a=*b
时间: 2023-07-23 10:05:29 浏览: 103
这段代码存在一些问题。首先,`int *a` 声明了一个指向整型变量的指针 `a`。然后,`*a = *b` 的语法是将指针 `b` 所指向的值赋给指针 `a` 所指向的内存空间。然而,在这段代码中,`*a=*b` 的右边并没有给出一个指针 `b`。
如果你想要将一个指针 `b` 的值赋给指针 `a`,你需要先声明并初始化指针 `b`,然后再进行赋值操作。例如:
```c++
int *a;
int *b = new int(10); // 假设 b 指向一个整型变量,并初始化为 10
a = b; // 将 b 的值赋给 a,即 a 和 b 指向同一块内存空间
// 现在可以通过 a 和 b 操作同一块内存空间的值
```
请注意,在使用完动态分配的内存后,需要使用 `delete` 运算符释放内存,以避免内存泄漏。
相关问题
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;
```
int *a,*b; *a=*b;*a=b;
这段代码存在一些问题。首先,变量a和b都是指向int类型的指针。在这段代码中,我们假设a和b已经被正确地初始化。
```
int *a, *b;
*a = *b;
*a = b;
```
问题出在第二行和第三行。第二行的`*a = *b;`尝试将b指针所指向的int值赋给a指针所指向的int值。但由于a和b均未初始化,这样的操作会导致未定义行为。
第三行的`*a = b;`试图将b指针赋给a指针所指向的int值,这同样是错误的。应该使用`a = b;`来将b的值赋给a,而不是使用`*a`来访问a所指向的值。
修正后的代码如下:
```c
int *a, *b;
// 初始化a和b
int value = 10; // 演示用值
a = &value;
b = &value;
*a = *b; // 将b指针所指向的值赋给a指针所指向的值
a = b; // 将b指针赋给a指针
// 此时a和b指向同一个int值
```
请注意,这只是修复了代码中的错误,实际上可能需要更多的上下文来理解您的意图,并进行适当的修改。