c - 使两个结构指针相等

标签 c pointers struct

假设我有一个名为 A 的结构指针和一个名为 B 的结构指针:

struct example{
//variables and pointers
}*A 

然后我有一个数据类型结构的指针示例:

struct example *B=malloc(sizeof(struct example));

如果我这样做

A=B;

这个算术运算是否意味着无论结构指针 B 指向什么,结构指针 A 也指向什么?我用原始数据类型和指针得到它,但结构让我感到困惑,因为它们内部有变量..

假设结构指针 A 已设置且所有内容

最佳答案

是的,对于以下代码行,A 应该指向 B 指向的相同位置。

/* Define the struct example datatype */
struct example{
    //variables and pointers
};

/* Declare A and B as pointers to struct example */
struct example *A;
struct example *B;

/* Allocate memory equivalent to the size of struct example
   and store the address in B */
B=malloc(sizeof(struct example));

/* Copy the address in B to A, so that A points to the same
   location B was pointing to */
A = B;

您应该将指针视为另一个 unsigned long 变量,它保存一个内存地址,(因为指针只是指向它保存的地址)。

与任何其他变量一样,您可以将存储在一个指针变量中的地址复制到另一个指针变量中。

关于c - 使两个结构指针相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533877/

相关文章:

go - 如何在 switch 语句中先声明结构然后再初始化它?

c - 我将哪个地址系列用于 src IPv4 和 dest IPv6?

python - 如何在 C 中使用 openssl bignum 将字符串转换为 long?

c - 如何在 C 中将 char 指针转换为 float?

c - 附加到链表的元素在附加后似乎发生了变化

C++ 类型转换指针指向常量变量

c++ - 如何在c中使用struct?

c - 错误: expected expression before ‘{’ token while assigning a character array

C语言类型转换

C:如何从未知大小的文本文件中仅输入 int 数据