我有一个下面的程序并且编译成功但是在运行时,程序在 eclipse 中崩溃了
struct Student
{
unsigned int *ptr; //Stores address of integer Variable
}*s1;
int main()
{
unsigned int roll = 20;
s1->ptr = &roll;
printf("\nRoll Number of Student : %d",*(s1->ptr));
return(0);
}
如何使用指向结构的指针打印 roll 的值
最佳答案
创建一个Student
结构,分配并使用
typedef struct Student
{
unsigned int *ptr; //Stores address of integer Variable
} Student;
int main()
{
Student *s1;
unsigned int roll = 20;
s1 = malloc(sizeof(Student));
if (s1 == NULL) {
return -1;
}
s1->ptr = &roll;
printf("\nRoll Number of Student : %d",*(s1->ptr));
free(s1);
return(0);
}
关于c - 理解指向结构的指针是指访问它时c中的指针成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44015332/