免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3148 | 回复: 14
打印 上一主题 下一主题

[C] [已解决]弱弱的问个菜菜的问题。。。指针做函数参数传递时。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-08 18:27 |只看该作者 |倒序浏览
指针做函数参数时。。。函数调用完毕后,函数中修改后的指针值可以保留么??
(我一直认为是可以的么。。)
main()
{
node head,end;
insert(m,n,head,end); (int ,int, node *,node*) node is struct
printf("head is %d %d\n",head->right1->n1,head->right1->n2);
printf("end is %d %d\n",end->n1,end->n2);
……
}

结果很奇怪::
head is 1 4
end is 138392 0  //这个貌似,end 指针越界了么。。可是不晓得原因。。。


在insert里输出的话,结果是正确的。。。
可是在main里就 是这样。。咋回事呢???

[ 本帖最后由 wenqing_9115 于 2009-6-9 15:19 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-06-08 19:11 |只看该作者
int insert(int a1,int a2,node head,node end)
{
        printf("*******the insert is %d %d ...\n",a1,a2);
   
        node x;
        x=malloc(sizeof(Node));
        node p_before;
        p_before=malloc(sizeof(Node));
        node p2;
        p2=malloc(sizeof(Node));

        x->n1=a1;
        x->n2=a2;
        x->left1=NULL;
        x->right1=NULL;
        x->left2=NULL;
        x->right2=NULL;


        if(head->right1==NULL)           //if it is the first one
        {
                head->right1=x;
                x->left1=head;
                end=x;
        printf("head is %d %d\n",head->right1->n1,head->right1->n2);
        printf("end is %d %d\n",end->n1,end->n2);

                counter=1;
                printf("the first one is %d %d \n",a1,a2);
        }
     
      ……
    return 0;
}

论坛徽章:
0
3 [报告]
发表于 2009-06-08 20:53 |只看该作者
你所指的“指针值”是指指针本身的指,还是指针指向的那个存储区的值?

论坛徽章:
0
4 [报告]
发表于 2009-06-08 21:38 |只看该作者
检查一下在insert函数中,printf("end is %d %d\n",end->n1,end->n2)语句后是否改变了end指向内存的内容

论坛徽章:
0
5 [报告]
发表于 2009-06-09 10:17 |只看该作者
原帖由 eveson 于 2009-6-8 20:53 发表
你所指的“指针值”是指指针本身的指,还是指针指向的那个存储区的值?



指针本身的值。。。

论坛徽章:
0
6 [报告]
发表于 2009-06-09 10:19 |只看该作者
原帖由 bbmmzz 于 2009-6-8 21:38 发表
检查一下在insert函数中,printf("end is %d %d\n",end->n1,end->n2)语句后是否改变了end指向内存的内容



这个我查过了么。。

insert 里printf后就return,main里接着就再printf的。。

论坛徽章:
0
7 [报告]
发表于 2009-06-09 10:31 |只看该作者

有经验的朋友,应该很容易知道吧??

如果int insert(int a1,int a2,node head,node end) 没有后面这两个指针参数就可以正常运行。
(typedef struct Node *node;)

外部变量
node head;
node end;

可是现在需要创建两个链表,像设置成函数参数来做。。可是传参,不知道那里出错了??

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
8 [报告]
发表于 2009-06-09 10:54 |只看该作者

论坛徽章:
0
9 [报告]
发表于 2009-06-09 10:57 |只看该作者

回复 #8 yangsf5 的帖子

好的。。我先看看。。多谢。。

论坛徽章:
0
10 [报告]
发表于 2009-06-09 11:07 |只看该作者
char *Str;
int key = -1;

CPUB_ResultMapServer::Getvalue(0, &key, &Str);  

Str本来就是指针,传参也传地址??这样编译都通不过么。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP