指针传递

560阅读 0评论2014-01-08 strongerII
分类:C/C++



  1. void *GetMemory(char **p)
  2. {
  3.     if(p == NULL)
  4.         printf("p = NULL\n");
  5.     *p = (char *)malloc(100);

  6.     printf("p = %p\n",*p);

  7.     return *p;
  8. }
  9. void main(void)
  10. {
  11.     char *str = NULL;
  12.     char *t = GetMemory(&str);
  13.     
  14.     printf("t = %p\n",t);
  15.     printf("str = %p\n",str);

  16.     strcpy(str, "hello world");
  17.     printf("%s\n",str);
  18. }
    将指针作为参数传递的时候,可以在函数里面更改指针里的内容,但是这个指针的内存地址不能被更改,如果要实现更改指针地址,必须传递指向指针的指针作为参数。
上一篇:人体时钟hone hone clock
下一篇:开源ftp