我有一个下面的程序并且编译成功但是在运行时,程序在 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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句