请教高手一个常量指针的问题!
代码是这样的:程序代码:
# include <stdio.h> # include <stdlib.h> #define bool int #define false 0 #define true 1 struct family { char name[20]; int age; }; bool fun(struct family const * no1, struct family const * no2); int main(void) { struct family no1 = {"tom", 10}; struct family no2 = {"sam", 20}; struct family * p1 = &no1; struct family * p2 = &no2; fun(p1, p2); printf("\n%d, %d\n", p1->age, p2->age); return 10; } bool fun(struct family const * no1, struct family const * no2) { printf("\nEnter %s's age: ", no1->name); scanf_s("%d", &no1->age, 1); printf("\nEnter %s's age: ", no2->name); scanf_s("%d", &no2->age, 1); /* no1->age = 60; no2->age = 80; */ return true; }
请问 const 将传递给函数 fun 的结构指针规定为常量, 那么结构 no1, 和 no2, 就是不能被修改的,那为什么在 fun 中使用scanf()可以改变成员的值?而注释部分的语句却不能修改成员值?