这个通过传参给结构体赋值的例子错在哪里了?【已解决】
本帖最后由 jessylake 于 2017-11-24 15:49 编辑using namespace std;
struct teacher
{
int id;
char name;
};
int get_mem(struct teacher** tpp)
{
*tpp = (struct teacher *) malloc(sizeof(struct teacher));
if (*tpp == NULL){
return -1;
}
*tpp->id = 100; //这句和下面那句为什么报错?上面不是指向结构体了吗?
strcpy(*tpp->name, "mike");
return 0;
}
void free_teacher(struct teacher **tpp)
{
if (*tpp != NULL){
free(*tpp);
*tpp = NULL;
}
}
int main()
{
struct teacher *tp = NULL;
get_mem(&tp);
cout << "id=" << tp->id << " name=" << tp->name << endl;
free_teacher1(&tp);
return 0;
}
终于知道,一疏忽把优先级给忘了
(*tpp)->id
因为->的优先级高于* 嗯嗯自己找出来的错误印象才深刻
页:
[1]