尝试使用指针将函数中的scanf值扫描到结构内的struct

时间:2015-08-18 00:21:12

标签: pointers struct

请查看代码,说明一切

username=knrast

它还没有完成,但是看看突出显示的行 - 当我使用指针(s-> DOB)扫描DateofBirth的值以在数据关闭后将数据保持在值时,我得到一个错误。 希望你能理解我的问题。

1 个答案:

答案 0 :(得分:2)

scanf要求您传递要存储结果的内存空间的地址。在函数中,无论您将指针传递给结构,都应该像这样使用scanf

void addDate(DOB_t* dob)
{
scanf("%d %d %d", &dob->d,&dob->m,&dob->y); //This would read an integer into the address of pointer plus the offset of member into the structure.
}

您在将值读入结构指针的每个位置都在代码中进行了此更改。