对于函数的传出参数,如果这个参数是通过函数体内部的变量赋值得来,它必须比函数体内的那个变量多一级指针,并且这个变量不能位于stack区。
eg:(错误之处欢迎指正)
阅读(90) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
eg:(错误之处欢迎指正)
点击(此处)折叠或打开
- #include<stdio.h>
- #include
-
- struct student {
- char *name;
- int num;
- };
-
- int func(struct student **stu)
- {
- struct student *std = malloc(sizeof(struct student)); //不能位于栈区
- if (!std)
- {
- printf("malloc error");
- return -1;
- }
- std->name = "chen_chuang";
- std->num = 3;
- *stu = std; //stu 必须 比 std 的指针多一级,原因很简单,函数的实参传入到到形参,形参只是从实参复制过来的,
- // 函数内部只是形参的改变,所以要想改变,只能改变实参指向的内容
- }
-
- int main(void)
- {
- struct student *sd;
- func(&sd);
- printf("student'name is %s\n",sd->name);
- printf("student'num is %d\n",sd->num);
- }
相关热门文章
给主人留下些什么吧!~~
评论热议