吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2008|回复: 2
收起左侧

[C&C++ 转载] 这个通过传参给结构体赋值的例子错在哪里了?【已解决】

[复制链接]
jessylake 发表于 2017-11-24 15:34
本帖最后由 jessylake 于 2017-11-24 15:49 编辑

[C++] 纯文本查看 复制代码
using namespace std;

struct teacher
{
 int id;
 char name[64];
};



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;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| jessylake 发表于 2017-11-24 15:48
终于知道,一疏忽把优先级给忘了
(*tpp)->id
因为->的优先级高于*
Try0oo 发表于 2017-11-24 19:49
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-2 18:32

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表