【发布时间】:2020-09-13 00:26:48
【问题描述】:
当我尝试从我的结构中访问年龄元素时(使用一个接受双指针的函数),我只能在第一次尝试时获得正确的值。为什么会发生变化?指针在移动吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
int age;
char name[200];
} student;
int getAge(student **s){
int a = (*s)->age;
return a;
}
student *create(){
student st;
student *mp = &st;
st.age = 25;
return mp;
}
int main()
{
student *sp = create();
int myAge = getAge(&sp);
printf("I am %d\n", myAge);
int Age = getAge(&sp);
printf("Again, I am %d\n", Age);
return 0;
}
【问题讨论】: