您的位置:首页 > 其它

指针的传参数与赋值问题

2014-03-10 21:13 204 查看
1.指针的赋值

eg:int (a[2]) [3];

则  int   *p1 = &a[0][0];

    int  (*p2) [3] = &a[0];

    int  (*p3)[2][3] = &a;

2.指针的传参问题(1)

  void get_m(int *p,int num)

  {

        p=(int*)malloc(sizeof(int)*num)

   }

   void test(void)

   {

           int *str=NULL;

           get_m(str,100);

           strcpy(str,"hello");

   }

 点评:对于该程序,一开始str指向空,然后把str的地址赋予p=00000000,然后p又malloc一块空间,假设p=12345678,然后就没了,值传递改变不了str的值,str的值仍然为空;

3.指针的传参问题(2)

 void get_m(int **p,int num)

  {

        *p=(int*)malloc(sizeof(int)*num)

   }

   void test(void)

   {

           int *str=NULL;

           get_m(str,100);

           strcpy(str,"hello");

   }

点评:对于该程序,一开始str指向空,然后把str的地址传给**p,既p指向str,*p=str=malloc一块空间,所以str指向malloc开的空间;

4.栈空间的释放问题

  char *get_(void)

  {

    char p[]="hello world";

    return p;

   }

   void test(void)

   {

       char *str=NULL;

       str = get_();

       printf("%s",str);

    }

点评:一开始str指向空,p数组对于数据段的hello world,因为p是在栈中所以引用完后p被释放;所以结果不确定,不能再引用;

5.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: