c++ - Struct 内部的指针

标签 c++ c typedef

我有这个代码:

typedef struct s1{
    void *x;
} s1;

typedef struct s2{
    unsigned char y[5];
} s2;

s1 *s1_g;
s2 *s2_g;

我想做的是将指针 x 指向变量 y。

我试过这样做:

s1_g->x = s2_g->y

但收到段错误

谁能教教我?

最佳答案

如果这就是所有代码,那么您甚至还没有初始化 s1_gs2_g。因此取消引用它们无疑会导致段错误。

关于c++ - Struct 内部的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20449886/

相关文章:

C++:在方法内部可以从类创建未初始化的对象吗?

Python C API 不加载模块

c - 在 C 中修剪字符串

c++ - std::result_of 应用于 const 重载方法

c++ - 什么是一个别名具有多种类型的 typedef?

c - lwip ip_addr - 存储大小

c++ - 关于 Windows 和 posix 中的 1 个刻度

c++ - 多重继承类的复制构造函数

c++ - upper_bound 和 lower_bound 取值要求不一致

使用 `pthread_create` 创建用户级线程或内核级线程?