免费注册 查看新帖 |

Chinaunix

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

? pointer [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-17 21:07 |只看该作者 |倒序浏览
Hi all,

I found the following function, which is used to remove the head of a
linked list.

void RemoveHead(node **head)
{
    node *temp;
    if (head && *head) { /* Corrected code */
        temp = (*head)->next;
        free(*head);
        *head = temp;
    }

}

1. Why is the purpose of 'head' in the 'if' condition? Shouldn't
'*head' suffice?

2. This function has an argument, which is a pointer to a pointer.
Presumably, 'head' is a pointer to '*head', which is another pointer
to the address of the link list head. If 'head' does not point to
NULL, does it follow that '*head' does not point to NULL? If so, why?

论坛徽章:
0
2 [报告]
发表于 2008-10-17 21:18 |只看该作者
1. 仅当head是一个有效的指针时,才可以解引用该指针(*head)
2. 不能推出。head指向一个指针变量,它的值不为NULL,但它指向的指针变量可以为任何值(即包括NULL)

论坛徽章:
0
3 [报告]
发表于 2008-10-17 21:35 |只看该作者

回复 #2 tyc611 的帖子

oh, thank you.
as the following example?

#include <assert.h>
  #include <stdio.h>
  #include <stdlib.h>

  static void showtext(const char **ptr);

  int
  main(void)
  {
      const char *text = NULL;
      const char **ptr = &text;

      assert(ptr != NULL);
      showtext(ptr);

      text = "Hello world";
      showtext(ptr);

      return EXIT_SUCCESS;
  }

  static void
  showtext(const char **ptr)
  {
      if (ptr == NULL)
          printf("invalid argument\n");
      if (*ptr == NULL)
          printf("a null pointer\n");
      else
          printf("a pointer to the text `%s'\n", *ptr);
  }

论坛徽章:
0
4 [报告]
发表于 2008-10-17 21:36 |只看该作者

回复 #2 tyc611 的帖子

$ ./a.out
  a null pointer
  a pointer to the text `Hello world'
  $

论坛徽章:
0
5 [报告]
发表于 2008-10-17 21:58 |只看该作者

回复 #3 bsd_lite 的帖子

you got it
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP