指向结构的指针包含指针
pointer to struct contains pointer
我正在用 c++ 编写代码,但我在指针方面有问题,请帮助我!
错误为:网格中0x010613af处未处理的异常.exe: 0xC0000005:访问冲突读取位置0x00000004
#include <iostream>
using namespace std;
struct test{
int test_num;
int * test_ptr;
};
struct test1{
int test1_num;
test* test1_ptr;
};
void main()
{
test1 tt;
tt.test1_num=0;
tt.test1_ptr=0;
int * t = tt.test1_ptr->test_ptr;
}
行:
int * t = tt.test1_ptr->test_ptr
正在取消引用空指针。在以下行中将其设置为 null:
tt.test1_ptr=0;
有两件事是错误的:
- main 必须返回一个 int。
-
您正在取消引用空指针。
int main() { test1 tt; tt.test1_num=0; tt.test1_ptr=0; int * t // = tt.test1_ptr->test_ptr; return 0; }
如果tt.test1_ptr
为0(即NULL),则不能尊重它,这是未定义的行为。
而不是tt.test1_ptr->test_ptr
你应该使用tt.test1_ptr.test_ptr
。请记住,运算符->
返回指针指向的实际值,在您的情况下,地址 0 没有值/您无权访问该值。
首先,您将test1_ptr
初始化为 0,然后在它指向任何内容之前尝试取消引用它。
tt.test1_ptr=0;
int * t = tt.test1_ptr->test_ptr;
相关文章:
- 如果基类包含双指针成员,则派生类的构造函数
- 包含矢量指针的结构的内存释放问题
- 指向包含对齐 C 结构C++类的 C 指针的对齐问题
- 包含指向其他结构的指针向量的结构
- 删除包含包含动态对象的 STL 容器的智能指针
- C++两个对象,其中包含指向同一数组不同部分的指针
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 为什么 Mac 上不需要包含智能指针?
- 指针数据类型变量如何包含对象?
- cppyy 继承包含智能指针的类
- 为什么 Wild 指针包含零地址而不是 garabge 地址?
- 为包含原始指针的对象C++智能指针
- 遍历包含 c++ 中指针的向量
- 如何在C++中重新实现包含指针的 STL 容器的类的迭代器
- C++对包含新指针的向量的分配
- 为什么此指针包含存储在堆上的地址
- 为什么指针包含一些垃圾
- 指向结构的指针包含指针
- 指针包含什么类型的数据
- 为什么变量指针包含相同数据类型的地址?