好吧,我正在阅读一本 C 书 Let us C,并且正在根据书中的图表表示阅读指向结构的指针,结构的指针指向结构的开头,例如
strcut b
{
char name[25];
char author[25];
int callno;
}b1,*ptr;
b1.name b1.author b1.callno
::::::::::::::::::::::::::::::::::::::::::::
: Let Us c : YPK : 101 :
::::::::::::::::::::::::::::::::::::::::::::
4001 4026 4051
ptr
::::::::::
: 4001 :
::::::::::
8000
上面是内存表示,其中4001,8000等是内存地址。现在如果默认情况下 ptr 指向 4001,那么不应该 b1.name 和ptr->name 有相同的内存地址,b1.author 和ptr->author> 具有相同的内存地址
最佳答案
这条记录
strcut b
{
char name[25];
char author[25];
int callno;
}b1,*ptr;
相当于
strcut b
{
char name[25];
char author[25];
int callno;
};
struct b b1;
struct b *ptr;
如您所见,如果这些定义定义了局部变量,则变量 ptr 未被初始化。如果他们定义了具有静态存储持续时间的变量(全局变量),那么 ptr 将被初始化为 NULL。
Shouldn't then b1.name and ptr->name have the same memory address and also b1.author and b1->ptr have the same memory address
如果原始定义写成这样的话,它们具有相同的地址
strcut b
{
char name[25];
char author[25];
int callno;
}b1,*ptr = &b1
也就是说,如果 ptr 是由 b1 的地址初始化的。
关于c - 指向结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28807306/