#include <stdio.h>
struct Student {
int id;
char name[20];
};
int main() {
struct Student stu;
struct Student\* ptr = &stu;
ptr->id = 101;
strcpy(ptr->name, "John Doe");
printf("Student ID: %d\n", ptr->id);
printf("Student Name: %s\n", ptr->name);
// explain the below code
printf("%d\n",(void*)ptr);
printf("%d\n",&stu);
printf("%d\n",*ptr);
return 0;
}
解释一下我注释过的代码的指针部分。 并且还要解释一下(void*)我很困惑 ptr 和 *ptr 具有相同的内存。
ptr
是引用结构体stu
的指针。
&
是“地址”运算符。 stu
的地址与ptr
指向的指针相同。
*
是间接(“指向的对象”)运算符。 *ptr
实际上是stu
。正如您所看到的,从技术上讲,您可以使用 %d
将其打印为整数,但这样做没有什么意义。