C语言中,调用成员变量用点还是用箭头,取决于当前的ID是指针还是结构体本身。
如:
typedef struct { float height; float weight; } Person; int main(int argc, char *argv[]) { Person jiushen; Person *lengleng = (Person *)malloc(sizeof(Person)); //注意这里结构体指针的声明,要分配空间 jiushen.weight = 60; jiushen.height = 175; lengleng -> height = 165; (*lengleng).weight = 50; printf("%f, %f\n", jiushen.weight, lengleng->weight); }
可以看出,对于指针lengleng,
lengleng -> weight
等价于
(*lengleng).weight
时间: 2024-10-10 00:32:16