做两个结构相同的指针 [英] making two struct pointers equal
问题描述
所以我们可以说我有一个结构的指针称为A和结构指针称为B:
So let's say i have a struct pointer called A and struct pointer called B:
struct example{
//variables and pointers
}*A
然后我有数据类型结构示例的指针:
and then i have a pointer of datatype struct example:
struct example *B=malloc(sizeof(struct example));
如果我这样做
A=B;
这是否运算意味着什么结构指针B的指向是什么结构指针A也指向?我得到它与原始数据类型和指针,但结构迷惑我,因为他们里面..
does this arithmetic operation mean that whatever struct pointer B is pointing to will be what struct pointer A is also pointing to? I get it with primitive datatype and pointers but struct confuse me because they have variables inside..
假设结构指针设置,一切
assume struct pointer A is set and everything
推荐答案
是与code以下线,应指向同一位置,B被指向。
Yes with the following lines of code, A should point to the same location, B was pointing to.
/* 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;
您应该把指针只是另一种无符号长
变量,它拥有一个地址到内存,(因为指针正好指向其持有的地址)。
You should think of pointers as just another unsigned long
variable which holds an address to a memory, (since the pointer just points to the address it holds).
正如任何其他变量,可以存储在一个指针变量的地址复制到另一个。
Just as with any other variables, you can copy the address stored in one pointer variable to another.
这篇关于做两个结构相同的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!