gpt4 book ai didi

c - 使两个结构指针相等

转载 作者:太空宇宙 更新时间:2023-11-04 06:03:07 25 4
gpt4 key购买 nike

假设我有一个名为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com